Posted about 7 years ago. Visible to the public. Repeats.

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 current.

From the git-config documentation:

push.default

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 upstream.
  • 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).

Copy
$ git config --global push.default matching

So to change that to push only current branches, just go ahead and say:

Copy
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.

Caveat with current

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 -u option:

Copy
git push -u

You can also run git push -u another time if you accidentally created a remote branch, but forgot to setup tracking.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
over 1 year ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more