1/14/2024[edited at: 1/16/2024]
Git rebase
Tags: git
You need to do 3-ways-merge to merge 2 branches that have new commits after branching. This creates a problem. The log becomes difficult to read and you probably don't know what's going on when there is a bug.
To solve this problem, you need to do rebase. git rebase
also merges 2 branches with new commits but makes them linear. To do this, git will add all of the new commits of one branch to the top of the other branch.
On this example, there are two branches, refactor and master. Below are the steps to rebase:
- You are on the master branch. To add all commits of refactor to master, you do
git checkout refactor
- Now, you're at refactor branch. Do
git rebase master
. Git rewinds HEAD back to the last common commit refactor has with master, applying all the commits from the master, make HEAD to be on top of them. Now, refactor is the same as master (hence, "rebase"). - Next, git applies all new commits from refactor. Now, refactor has the same history with master, only that it is ahead of master 3 commits.
- Do
git checkout master
to switch back to master branch, thengit merge refactor
to do the fast-forward merge with the refactor. - Finally, your log is linear.