When we need to join two or more branched together, we have two git commands can choice — Merge or Rebase.
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)
When 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:
- Merge
Commit IintoFeature->Commit X - Fix the conflicts ->
Commit Z - 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 mergeMasterbranch intoFeature?
No! I just want to avoid the conflicts in my pull request. The merge from Master to Feature is unnecessary. So, here has another option — Rebase.
The Rebase Option
Rebase can change the node that you branch out, you can compare the following graph with previous:
- Rebase
MasterinFeaturebranch. - If has conflict, fix it then use command
rebase --continue. - 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.
Notice: BecauseRebasecommand will change the node you branch out, if the information of the branch out node is important, maybe you should not useRebaseto join your branches.