9

The problem I've had is that my fingers automatically type git push origin master. If I instead am working on new-branch and was meaning to type git push origin new-branch and there are unpushed changes on master they will be pushed accidentally. Is there some way to prevent this so that I can only push to and from the current branch?

6 Answers 6

18

You can specify what git push does if no refspec is given using the push.default setting in git-config. There are three modes for your situation:

The simple, current and upstream modes are for those who want to push out a single branch after finishing work, even when the other branches are not yet ready to be pushed out.

Note that this setting only takes effect if no refspec is given, i.e. running git push without further arguments. git push origin master will still push master to origin.

As a rule of thumb you should always create separate working branches and never commit intermediate results to your master branch. git flow simplifies this workflow.

From the 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 branches having the same name in both ends. This is for those who prepare all the branches into a publishable shape and then push them out with a single command. It is not appropriate for pushing into a repository shared by multiple users, since locally stalled branches will attempt a non-fast forward push if other users updated the branch. + This is currently the default, but Git 2.0 will change the default to simple.

  • upstream - push the current branch to its upstream branch (tracking is a deprecated synonym for this). With this, git push will update the same remote ref as the one which is merged by git pull, making push and pull symmetrical. See "branch..merge" for how to configure the upstream branch.

  • simple - like upstream, but refuses to push if the upstream branch's name is different from the local one. This is the safest option and is well-suited for beginners. It will become the default in Git 2.0.

  • current - push the current branch to a branch of the same name.

3
  • 1
    Also if you're not interested in changing the behaviour of git push without paramteres, you can set up upstream branches for all branches and use git push HEAD which pushes the currently checked out branch. Aug 17, 2012 at 10:28
  • @MarcinK ah that's clever as well, unless you have been moving between branches and HEAD is somewhere else. Aug 17, 2012 at 15:06
  • @Adam I understood from your question, that you always want to push the current branch. And HEAD is always at the current branch. Aug 19, 2012 at 7:56
9

You want to upgrade git to version 1.7.11 or later, and use the "simple" push.default:

o    simple - like upstream, but refuses to push if the upstream branch's name is different from the local one. This is the safest option and is
     well-suited for beginners. It will become the default in Git 2.0.

This will precisely prevent your issue. Note it will also become the default option in Git 2.0. You can enable it with:

git config push.default simple
5
  • This sounds exactly like what I am looking for. I will try it out in a bit :) Aug 17, 2012 at 13:28
  • Actually this is not a solution, git push origin master still pushes master->master instead of doing nothing. Aug 17, 2012 at 15:02
  • Ah yes, no default option will prevent you from explicitly pushing a specified ref, but with this enabled you can issue git push without any argument and have it work correctly on a branch that tracks a remote branch. Aug 17, 2012 at 15:06
  • I suppose it's just how you want to retrain your fingers. Would you like to use a git alias, or not use arguments on the command? Aug 17, 2012 at 15:08
  • What I will probably end up doing is creating an alias that forces me to type mygitpush remote branch but disables pushing to non-active branches, and then hide git push. I like being forced to be explicit, otherwise I'll mess up. Aug 18, 2012 at 14:20
5

You can just create an alias named, say, gitpushcurrent, pointing to git push origin $(git branch | grep "*" | sed "s/* //").

Then retrain your fingers to type automatically gitpushcurrent instead of git push origin master. Thus you don't need to think what is the current branch.

1
  • This was my first thought, but figured there might be a built-in way of doing it. As far as I can tell this is the best solution. Aug 17, 2012 at 15:05
1

I'm using Git by few days, but, by the way, you can:

  1. Pay attention to what you're typing with the keyboard. Git is powerful, and, if your fingers are well trained to type anything automatically, you could make a lot of damage, such as deleting your working branch, or merge branches when you don't want it. Always be careful.

  2. Use aliases for your commands. They're very powerful and can save you time, preventing you to make something you don't really want.

  3. Read some interesting and useful topics, like this SO question or this one on ServerFault.

0

git -g config push.default current

0

This question is very similar to "Confirmation of git push command"

And, I am going to propose the same solution here too.

All you need is the pre-hook for the commits into protected branches like "master".

This blog on "how-to-prevent-push-to-master" should help you.

PS: GIT Version 1.8.2 or above needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.