Default behavior of push.default in git

To find your git version execute command git --version.

If your git version is “2” or higher then you can skip this step. If not then please read on.

Execute the following command on your terminal.

$ git config --list | grep push

If the result, as shown above, is current then you’re all set. If not, read on.

What we’re discussing

Imagine this scenario:

  • John is working in 184-enable-facebook-authentication.
  • He made two commits in 184-enable-facebook-authentication.
  • Then he was asked to look into the 185-build-sales-report branch.
  • He made one commit in 185-build-sales-report.
  • Then he switched back to 184-enable-facebook-authentication.
  • …and in this branch, after rebasing, he did a force push. The side effect of this force push was that he accidentally also force pushed to 185-build-sales-report and to the master branch.

How did this happen? Because the default setting of push.default was matching in his environment . This is the default setting in git 1.x.

Here’s how it works

When the push command is issued, what happens next depends on the set value of push.default. Here are the options:

  • nothing: This ignores the push request.
  • matching: This will push all matching branches from local to remote. This is the default setting, and the reason for John’s mishap.
  • upstream: This pushes the current branch to the upstream branch.
  • current: This pushes only the current branch to its matching name.

The default should be changed to current. You can do this by executing the following command in the terminal:

git config --global push.default current