The answer is git rebase!

Supposedly that the man who works on the red branch has already made 4 commits locally. Suddenly the colleague who was working on the green branch informs him that he has updated the remote master by pushing his changes there and that he should check the new changes out now. 

In case you are the man who works on the red branch, the first thing to do is go and checkout the master branch and then pull from the remote master (git pull origin master). If you have uncommited or even unstaged files then git prevents you from going out of your current branch until you commit them or discard them. The most usual hack here is to temporarily save them to memory by applying git stash. Then you are free to go to master.

Once you pull from origin master and your local master has your colleague's updates, it is time to think how are you going to introduce these changes from master into your branch. One way is to merge master into your branch, but what if you want to keep yours  on top, so the commits appear chronologically exactly as they happened? 

You switch to your master. If everything is clean you proceed. If you have stashed files, then you need to unstash them now by git stash pop.  This action will make this files again available to your branch ready to be added and committed. When you commit and your working directory is clean and there is nothing to commit you can run git rebase master.  This will create a new commit on top of your local commits that will fetch and merge everything from master and then it will copy all your previous local commits up to this point that master does not have and will put them on top of it. 

Attention: That means that it will recreate 4 new commits that are identical with the first ones but they have different hash (different commits). 

If there are conflicts you resolve them manually in your editor. Then you add everything to staging area and then you type git rebase --continue. 

That's it! Now you have all the changes that have been made by others locally to your branch and your commits on top of them. So when you feel ready the only thing you have to do is merge to your local master and then push to origin master.

Before Rebase flowchart

Kostas Diakogiannis
Module by Kostas Diakogiannis, updated more than 1 year ago
No tags specified