Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent git push <branch> from pushing unless I am checked out on that branch?

Tags:

git

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?

like image 304
Adam Bergmark Avatar asked Aug 17 '12 10:08

Adam Bergmark


People also ask

How do I stop a branch from pushing?

To create a branch protection rule, navigate to your repository's settings. Click Branches and then Add rule under the “Branch protection rules” section. Enter “main” under Branch name pattern. Then check Require a pull request before merging.

What would you use to prevent force pushes in git?

Use Protected Branches If you're hosting your remote repository on GitHub, you can use GitHub's Protected Branches. By default, GitHub will block force pushes on all protected branches, so you will just need to decide which branches should be protected.

Does git push automatically push to current branch?

If you run the simple command git push , Git will by default choose two more parameters for you: the remote repository to push to and the branch to push. By default, Git chooses origin for the remote and your current branch as the branch to push.


2 Answers

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.

like image 61
Stefan Avatar answered Nov 09 '22 22:11

Stefan


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
like image 36
Christopher Avatar answered Nov 09 '22 22:11

Christopher