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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With