How to merge remote changes at GitHub?
Asked Answered
T

7

136

I'm getting following error, whn trying first Github push:

[rejected] master -> master (non-fast forward)
error: failed to push some refs to '[email protected]:me/me.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'non-fast forward'
section of 'git push --help' for details.

how can I fix this and merge remote changes?

Terrific answered 11/11, 2009 at 5:3 Comment(0)
W
108

See the 'non-fast forward' section of 'git push --help' for details.

You can perform "git pull", resolve potential conflicts, and "git push" the result. A "git pull" will create a merge commit C between commits A and B.

Alternatively, you can rebase your change between X and B on top of A, with "git pull --rebase", and push the result back. The rebase will create a new commit D that builds the change between X and B on top of A.

Waylen answered 11/11, 2009 at 5:3 Comment(3)
I keep running into this except that if I do a "git pull" I am told "Already up-to-date." and if I do a "git pull --rebase" I'm told that "Current branch master is up to date." Any ideas? Thanks!Tope
@larson4 I got the same issue, but after you do the git pull, do another commit and then it should be goodEnumerate
@Enumerate @larson I had a similar problem caused by the fact that I did not read the error message carefully. The reject was on a branch that I didn't have checked out. The branch I was actually on was succeeding. The solution was to git checkout other-branch; git pull; git push; git checkout branch-i-was-working-on.Silici
B
88

You can also force a push by adding the + symbol before your branch name.

git push origin +some_branch
Brash answered 11/11, 2009 at 5:3 Comment(6)
Thank you, this one works for me. The other solutions would completely erase the effect of my git reset --hardBarretter
Thanks! I had pushed branch "A" up to my Heroku staging app to test some functionality in a production environment. Then (locally) I merged "A" and "B" into "master" and wanted to push "master" into my staging app. Was having all sorts of problems. This made pushing the "master" very simple. Thanks!Flagler
Worked for me too. Took me 4 hours to find the issue. Thanks a lot. I run Netbeans on local machine (windows 7) and wanted on each push on local, check out on remote machine (linux).Enchiridion
Note that this method might not be safe, and it can cause some diverged commits to be unreachable.Superintend
This should be the accepted solution. The other solutions were not working for meWindle
I agree. this is the better solution. The comment thread above about just re-committing the file, still displayed the same error.Rodger
T
20

You probably have changes on github that you never merged. Try git pull to fetch and merge the changes, then you should be able to push. Sorry if I misunderstood your question.

Tuppeny answered 11/11, 2009 at 5:3 Comment(1)
In case you need to reject changes in the remote master and push your own changes, try to push with -f keyBryson
Y
13

If you "git pull" and it says "Already up-to-date.", and still get this error, it might be because one of your other branches isn't up to date. Try switching to another branch and making sure that one is also up-to-date before trying to "git push" again:

Switch to branch "foo" and update it:

$ git checkout foo
$ git pull

You can see the branches you've got by issuing command:

$ git branch
Yentai answered 11/11, 2009 at 5:3 Comment(2)
Can you explain why this works and is necessary? (It did solve my problem.) It just seems counter-intuitive to me. I don't understand why git would need another branch to be up to date as well for me to push on the master branch.Akerley
@QuinxyvonBesiex I'm not sure I myself understand. It may have something to do with the underlying structure of Git itself, and how it organizes branches (which are basically the same as tags as far as I understand it).Yentai
R
7

You can force it to push, but please do this ONLY when you're quite sure what you are doing.

The command is:

git push -f 
Roeder answered 11/11, 2009 at 5:3 Comment(0)
M
3

This problem can also occur when you have conflicting tags. If your local version and remote version use same tag name for different commits, you can end up here.

You can solve it my deleting the local tag:

$ git tag --delete foo_tag
Mock answered 11/11, 2009 at 5:3 Comment(0)
L
2

When I got this error, I backed up my entire project folder. Then I did something like

$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master

...depending on your branch name (if it's not master).

Then I did git pull --rebase. After that, I replaced the pulled files with my backed-up project's files. Now I am ready to commit my changes again and push.

Leggy answered 11/11, 2009 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.