How to Change Git Branches on a Site Deployed with Forge

Categories: Development, Tools

When Forge provisions a site, it uses a shallow clone of a single branch. This method prevents switching to other branches later. This approach is helpful for performance. Nonetheless, it can be limiting. This limitation arises when you need to test or deploy from a different branch on a staging or test server.

Here’s how Forge typically clones a Git repository

git clone --depth=50 --single-branch -b staging [email protected]:adampatterson/repo repo

If you check the remote branches:

git branch -r
# origin/staging

This setup locks the repository to the staging branch, as seen in the .git/config:

[remote "origin"]
    url = [email protected]:adampatterson/repo
    fetch = +refs/heads/staging:refs/remotes/origin/staging

Switching to Another Branch

To allow access to all branches, update the fetch configuration:

git config remote.origin.fetch "refs/heads/*:refs/remotes/origin/*"
git fetch

Your .git/config should now look like:

[remote "origin"]
    url = [email protected]:adampatterson/repo
    fetch = refs/heads/*:refs/remotes/origin/

Now you can view all remote branches:

git branch -r
# origin/dev
# origin/main
# origin/staging

To switch to a different branch:

git fetch origin
git switch main

If you want to lock the repository to a new default branch (e.g., main), reset the fetch:

git config remote.origin.fetch "refs/heads/main:refs/remotes/origin/main"


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!