Sunday, 22 April 2018

Git Merge Strategies Part 1: Rebasing

Git Rebase




Git provides two merge strategies: 

1. Merge : It does a 3-way merge in case of diverse history. {it can also do a fast-forward merge, if histories were linear.}. Dont worry about history and some technical terms for now, you are fine trust me!! We will talk about it in an entirely different blog.

2. Rebase : It's like saying take all my local commits and fetch the origin new changes and apply my local commits now one by one to the tip of the origin branch.

Use case: 

1. If your working on the centralized workflow. and following is the situation. 

a. Origin master has  1--2--3 commits.
b. You checkout the branch and start writing your local commits i.e "a--b--c" on top of 3 locally. Note: You haven't push them on origin. Its worth noticing that right now your Base is "3" commit.
c. Now somebody pushed commit 4 and 5. So now the master looks like 1--2--3--4--5.
d. Now when you will try to push your 3 commits  a--b--c on the master. The git will throw an error message. Saying that the remote has some new changes which your local branch doesn't have.

Now comes the action: Now to pull the origin changes and merge them into your local branch you have two ways to do it.

1. Git pull [which is a combo of "git fetch" and "git merge"]. Remember "Merge" is one of the ways to bring the code and its a 3-way merge strategy which will endup generating an auto-generated commit. Which in cases, is un-necessary and will make your official project history very dirty with time.

Commands to issue:
git pull
// now push to the remote
git push

End result of this method: 1--2--3--4--5--6--a--b--b--merge_autogenerated_commit

2. Rebasing comes to rescue here, Its simply saying like, "Put all my local commits aside and bring the remote new changes and then apply my commits on top of it, one by one."
Note: Conflict merging is out of scope of this discusstion for now.

Commands to issue:
git pull --rebase
git push



Recap:

1. When you just want to pull the remote part of the same branch:
git pull --rebase origin <branchname>  : You can skip the origin <branchname part if branch tracking is already setup.>

2. When  your on a feature branch and you want to rebase on the master. ie. when you want to rebase on another branch
git rebase <source-branch> <sink-branch> : current branch is optional, when you give the branch name it becomes a combo command of git checkout <sink-branch> and then git rebase <source-branch>

e.g If you are on feature-a branch and want to rebase this branch with the top of master. then the command will look like
git rebase master

if you are not checkout out on feature-a branch then you can issue a combo command
git rebase master feature-a


Some technical insight:

Rebase guarantees fast-forward merging because the history of the two branches we want to merge shall be rebased before merging the commits hene generating a linear history.



There are advances rebasing too, but probably you won't use it everyday life. So I will just not re-iterate it, please visit the following links to learn more.
Source:
https://git-scm.com/docs/git-rebase
https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase


No comments:

Post a Comment