Toxic Elephant

Don't bury it in your back yard!

Tracking a git branch with a different name both ways

Posted by matijs 24/05/2025 at 08h49

You want to work on some project, but they use a branch named dog and you want to use cat instead. So you go:

git checkout dog
git switch -c cat
git branch --set-upstream-to=origin/dog
git branch -d dog

Your local branch is now cat and it tracks dog. Whenever your dog-loving friends push their work, you can just use

git pull --rebase

and your cat branch will be updated with the new commits in origin/dog.

Now, the time comes to push your own work, and you go:

git push

Now, however, something perhaps unexpected happens. Instead of your commits getting pushed to dog, a new branch cat is created in the remote repository. That’s not what you wanted!

To make this work properly, you have to change git’s configuration:

git config set --global push.default upstream

From the git config man page, this setting will:

push the current branch back to the branch whose changes are usually integrated into the current branch

In other words this pushes to the branch set with --set-upstream-to, which normally only affects pulling.

Now git push will push your cat branch to the remote origin/dog branch, allowing cat people and dog people to work together peacefully.


PS: With this push setting, pushing a branch without an equivalent upstream will fail. To instead make git push create an upstream branch for you, set:

git config set --global push.autoSetupRemote true

Tags , no comments no trackbacks