How do I synchronize two branches in the same Git repository?

Viewed 53690

Here's a common workflow hurdle I encounter often:

master is our "stable" branch

$ git status
# On branch master
nothing to commit (working directory clean)

Create a module on a branch

$ git checkout -b foo
$ echo "hello" > world
$ git add .
$ git commit -m "init commit for foo module"
$ git checkout master
$ git merge foo

Do work on master or other branches

Over the next couple weeks, more code will be committed to master directly and by other branches. The foo branch will go untouched for this time period.

Resume work/make updates on the foo branch

$ git checkout foo

Oh no! foo is massively out of date! I understand why, but I do need foo back in sync.

The question

How do I get the latest contents from the master branch?

4 Answers

I use the following to combine changes from two branches (mine and yours) and to synchronize both branches for continued work. This seems to be working. Does anyone see a problem with it?

git checkout mine # make sure I'm on my branch
git commit -a     # commit changes
git push origin mine  
git checkout yours # switch to your branch
git pull origin yours # get changes you've committed & pushed
git checkout mine 
git merge yours # merge your changes into mine
git push origin mine 
git checkout yours 
git rebase mine # set your branch to the merged result
git push origin yours # push the merged result up to your branch on origin
git checkout mine # get back to my branch

Adam answer is good one.

Additionally, for those who are using IntelliJ and need help regarding rebase and merge, here is a link of youtube video. This will help you to visually understand how rebase works.

https://www.youtube.com/watch?v=Nftif2ynvdA

Related