How to Avoid Having to Do “Git Branch --set-upstream”, and Instead Default to Automatically Setup Remote Tracking?
To avoid having to manually set the upstream branch for each new branch you create and instead default to automatically set up remote tracking, you can configure Git to always set up remote tracking by default when creating new branches.
Configure Git to Auto-Track Remote Branches
Global Configuration:
You can set this behavior globally so that it applies to all repositories on your machine:
Repository-Specific Configuration:
If you want to set this behavior for a specific repository only, run the commands without the
--globalflag inside your repository:
Automatically Set Upstream Branch with git checkout or git switch
With the configuration set, new branches created with git checkout -b or git switch -c will automatically set up tracking branches. For example:
When you push the new branch to the remote for the first time, use:
After the initial push, subsequent pushes and pulls will track the remote branch automatically.
Example Workflow
Create and switch to a new branch:
Make some changes and commit them:
Push the new branch to the remote repository:
After these steps, your branch feature-xyz will be automatically set to track origin/feature-xyz, and you won't need to run git branch --set-upstream manually. Future pushes and pulls will be simpler and will automatically interact with the remote branch.