Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git push branch error

Tags:

git

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 [email protected]:brabbit/projectA.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]: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.

like image 627
BRabbit27 Avatar asked Dec 26 '22 11:12

BRabbit27


2 Answers

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.

like image 55
John Szakmeister Avatar answered Jan 05 '23 13:01

John Szakmeister


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
like image 31
Ja͢ck Avatar answered Jan 05 '23 13:01

Ja͢ck