Reputation: 7566
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?
Upvotes: 9
Views: 7142
Reputation: 1645
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.
Upvotes: 0
Reputation: 114248
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
andupstream
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 tosimple
.
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 bygit pull
, makingpush
andpull
symmetrical. See "branch..merge" for how to configure the upstream branch.
simple
- likeupstream
, 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.
Upvotes: 19
Reputation: 44304
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
Upvotes: 9
Reputation: 8227
I'm using Git by few days, but, by the way, you can:
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.
Use aliases for your commands. They're very powerful and can save you time, preventing you to make something you don't really want.
Read some interesting and useful topics, like this SO question or this one on ServerFault.
Upvotes: 1
Reputation: 9922
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.
Upvotes: 5