Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git fetch/merge doesn't work

Tags:

git

I made some changes to the code.

  • git add -A
  • git commit -m ".."
  • git push origin master

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

  • git fetch origin master

remote: Counting objects: 42, done. remote: Compressing objects: 100% (26/26), done. remote: Total 26 (delta 17), reused 0 (delta 0) Unpacking objects: 100% (26/26), done. From ...... * branch
master -> FETCH_HEAD

  • git merge master

Already up-to-date.

  • git merge origin/master

Already up-to-date.

  • git merge origin/master master

Already up-to-date. Yeeah!

  • git push origin master

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

So the same again.

What's going on? With git pull it works, but I read it's better to use fetch/merge, so please tell me what am I doing wrong.

like image 318
Andrzej Gis Avatar asked Aug 10 '26 10:08

Andrzej Gis


2 Answers

git pull will lead to exactly the same result as a git fetch and git merge.

git fetch works a bit different depending how you call it. Usually you just call git fetch. This will update all remote branches from origin. Afterwards your own branch is still master while the remote branch is origin/master, hence if you want to merge origin/master into master you have to checkout master and git merge origin/master.

git fetch origin master will just fetch that one branch and store it into a temporary branch called FETCH_HEAD. you could merge it using git merge FETCH_HEAD.

But you probably only want to do a simple git pull. ;)

Hint: use git branch -av to see all your local and remote branches.

like image 91
michas Avatar answered Aug 13 '26 09:08

michas


What branch do you have checked out? git merge origin/master master does not target master: You always merge to the current branch.

like image 25
Ben Jackson Avatar answered Aug 13 '26 11:08

Ben Jackson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!