Laravel Forge: Branch or Remote Change fails
When you change the git remote or the deployed branch via Laravel Forge, a stubborn issue comes up time and again: the repository cloned on the server does not recognize the new branch.
The deployment fails, or Forge keeps using the old branch.
The cause is a too narrowly configured remote.origin.fetch setting.
The official Laravel Forge documentation does not mention this case and treats a branch change as a simple dropdown selection.
When does this problem occur?
You changed the repository or the branch of the site in Forge.
The deployment script reports errors like "pathspec did not match any file(s) known to git", or Forge keeps pulling commits from the old branch.
A manual git fetch on the server does not pull any branches other than the one originally configured.
Why does the server not recognize the new branch?
Forge often creates the repository on the server with a restricted refspec that only includes the originally selected branch. This refspec is stored in the local .git/config as remote.origin.fetch. When you later switch the branch or the remote via the Forge UI, Forge updates the site configuration. The fetch rule inside the repository on the server, however, stays untouched. The result: git fetch pulls nothing other than the originally configured branch.
How do you check the current configuration?
Connect to the Forge server via SSH, switch to the site directory and check the currently configured refspec:
git config --get remote.origin.fetchLanguage:shell
A restricted setup returns a line like this:
+refs/heads/private_dev_branch:refs/remotes/origin/private_dev_branchLanguage:shell
In that case, git only fetches the originally selected branch. Every other branch in the repository stays invisible to the server.
How do you fix the problem?
Set the refspec to the default pattern, which includes every branch:
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"Language:shell
Then verify the result:
git config --get remote.origin.fetchLanguage:shell
Expected output:
+refs/heads/*:refs/remotes/origin/*Language:shell
A final git fetch origin --prune updates every branch. The new branch can then be selected via a deployment in Forge or directly with git checkout <branch-name>.
How can you prevent this in the future?
If it is already clear that the site will need multiple branches or that the remote may change later, it pays off to set the refspec to the default pattern right after provisioning. That prevents exactly this problem over the remaining lifecycle of the server. For projects we operate long-term, we document this step alongside the other server configurations so it does not get lost during handovers.