Git - Rebase Or Merge ?
When we need to join two or more branched together, we have two git commands can choice —
The Merge Option
In the past, I always using
merge to join my branches:
A ------> B ------> C (Feature Branch)
F -> G ---> H -----> I ----> J (Master Branch)
Commit D merge to
Commit J , occasionally occur some conflicts, if it is your own project, you can just fix the conflicts and commit again.
But if it is someone’s repos and you want to create a pull request, than your pull request will be rejected because the conflicts.
In this case, I need to do the following steps:
- Fix the conflicts ->
- Create a pull request again. (Now the conflicts has fixed)
A ---> B ---> C --> X - > Z (Feature Branch)
/ / \
F -> G -----> H -------> I ------> J (Master Branch)
As you see, the log graph will be more complicated, and if the
Master branch is very active, this can pollute
Feature branch’s history quite a bit.
Did I really want to merge
No! I just want to avoid the conflicts in my pull request. The merge from
Feature is unnecessary. So, here has another option —
The Rebase Option
Rebase can change the node that you branch out, you can compare the following graph with previous:
- If has conflict, fix it then use command
- Create a pull request.
A ---> B ---> C (Feature Branch)
F -> G -> H -> I --------------> J (Master Branch)
Now we avoid the unnecessary merge and get a more cleaner log graph.
Rebasecommand will change the node you branch out, if the information of the branch out node is important, maybe you should not use
Rebaseto join your branches.