Git: How to configure git to push only your current branch
You can change which branches will be pushed when saying
git push. Our recommendation is to set it to
Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:
nothing- do not push anything.
matching- push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.
upstream- push the current branch to its upstream branch.
tracking- deprecated synonym for
current- push the current branch to a branch of the same name.
If you want to check your setting, do the following. By default, it will return
matching (see above).
$ git config --global push.default matching
So to change that to push only current branches, just go ahead and say:
git config --global push.default current
Mind that omitting the
--global flag only changes it for the current repository. Since it's hard to remember which project behaves how, you should find a suitable global setting.
Regardless which option you use you can manually do a
git push origin my-branch to explicitly push only your current branch.
For most of us,
current is the safest push behavior. However, it has a small side effect: When a branch with your local name does not exist on your remote, it automatically creates it on the remote.
While that is usually what you want, it does not setup tracking between your local and remote branches. That means
git pull won't work, and Git won't notify you when your local and remote branches differ.
To create a branch with the same name on the remote and setup tracking, use the
git push -u
You can also run
git push -u another time if you accidentally created a remote branch, but forgot to setup tracking. If you can't currently push, use
git branch --set-upstream-to=origin/$(git branch --show-current).