简体   繁体   中英

Git push branch error

The scenario is simple. From the master I have a branch called myBranch . All the time I'm working in the latter. Once I've done with some modifications I want to push my changes from my local copy of myBranch to the remote branch myBranch . Every time I try this I get the following error/warning message

To git@github.com:brabbit/projectA.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:brabbit/projectA.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

So I proceed with a git pull which runs fine. Then try again with git push and the same error appears.

Could you explain me what does the message means and how to solve it. I am quite new to git.

You need to change the default push behavior. The branch being complained about is master , which doesn't appear to be the branch you are working on. By default, Git treats push.default as matching , which means that when you say git push , it will push every branch that goes to origin. Unfortunately, most people don't keep all their local versions of branches up-to-date, which will result in the error that you see above.

It happens because there are new commits on the master branch in the remote repository, but your local version of master doesn't have them. When you push, Git is trying to make the remote master branch match your local version. Since the local version is behind by several commits, this is a non-fast-forward update (you would lose history) and Git complains.

I generally recommend that folks change the push.default setting right away to either upstream , simple , or current :

git config push.default upstream
git config push.default simple
git config push.default current

You can read about the difference it the git-config man page (search for "push.default"). You can also see it at the command line with:

git help config

Once set, git push will only push the branch that you are on, and this message will go away.

Also, if you're debating on using git push --force , don't consider it until after you've changed your default push setting. It's a great way to lose history, and it's also the reason why Git will change the default behavior in 2.0--though I think they should've done it already.

You're still on the branch myBranch , so you need to specifically say that you only want to push that branch:

git push origin myBranch

Or, you can configure to always push the branch that you're on by default:

git config push.default current

After that, to fix the master branch:

git rebase origin/master master
git push origin master

It looks, like your branch didn't configured well for git pull . In case you just want to push a bunch of simple commits (no merge commits) I would suggest using git rebase :

git checkout master
git rebase origin/master
git push

In case your changes contain merge commits and you want to push it, like you see it in the history ( gitk --all ) you can do

git checkout master
git merge origin/master
git push

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM