0

When we do a fetch or pull and the local master branch is behind the origin/master branch or if the local master is on separate branch compared to origin/master, then how to bring local master in sync with origin/master?

https://git-scm.com/book/en/v2/images/remote-branches-3.png

variable
  • 4,701
  • 3
  • 34
  • 85
  • did you had a look to `git pull` ? – OznOg Oct 10 '19 at 19:38
  • That's what `git pull` does. It is a fetch + a merge (or rebase). Are you finding something different? Pulling is covered just a bit further in [that document](https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches). – Schwern Oct 10 '19 at 19:39
  • 1
    Possible duplicate of [master branch and 'origin/master' have diverged, how to 'undiverge' branches'?](https://stackoverflow.com/questions/2452226/master-branch-and-origin-master-have-diverged-how-to-undiverge-branches) – OznOg Oct 10 '19 at 19:41

1 Answers1

2

You can perform the merge (or rebase) manually just like any other branch.

git fetch origin
git checkout master
git merge origin/master

0b743 < a6b4c < f4265 < 31b8e < 190a3 [origin/master]
                     \               \   
                      < a38de < 893cf < abc123 [master]

Or you can use git pull origin master which will git fetch origin and git merge origin/master for you same as above.

git checkout master
git pull origin master

Or you can avoid an unnecessary bookkeeping merge and replay your local changes on top of the remote ones with git pull --rebase. This is a git fetch origin plus a git rebase origin/master.

git checkout master
git pull --rebase origin master

0b743 < a6b4c < f4265 < 31b8e < 190a3 [origin/master]
                                     \   
                                      < a38de1 < 893cf1 [master]
Schwern
  • 127,817
  • 21
  • 150
  • 290
  • While this answer presents rebasing as though it were a no-brainer, there actually are pros and cons to rebasnig on pull. Understand that immediately after a rebase, *all* of the rewritten commits that replace your local commits are untested - not just the final result, as with a merge – Mark Adelsberger Oct 10 '19 at 20:19