MERGE & REBASE
- Both of these commands are designed to integrate changes from one branch into another branch
- They just do it in very different ways.
https://gitforteams.com/resources/merge-rebase.html
Merge problems
! Create unnecessary merge commit
- In every feature branch, there are “merge commit”.
- What if there are many developers who share the main branch and the size of the project is large?
- Branch history can be noisy.
(= Even if a logic is created and modified in an independent branch, it becomes difficult to distinguish between other tasks and their history.)
Rebase
! Clean up unnecessary merge commit
! Gathering commits that have done the same work
Rebase changes the base of commit
= Organize the commit history in a line.
To check the base commit of the branch : git merge-base main feature/sign-in
(Not necessary)
Rebase Process
1. Rebase - Before push the branch to main…
git pull origin main
git checkout feature/branch
git rebase -i main
2. Squash
Commit Message Cleanup && Integration
-
pick
: Pick the base point commit
- Based on which commit do you clean up other commit?
- Pick the oldest commit!
- top → bottom === oldest → latest
- It does not mean that other commit's work history will be lost.
-
s
: Squash commits
-
:wq
3. Resolving conflict in rebase
- Conflict can occurs when the contents are different between Commit.
- During the rebase, many commits can raise conflict at once.
- If a conflict occurs, the rebase does not proceed or end, it just stops in the middle.
- 👍 On terminal, we can see a message
(rebase 1/2 ~1)
that inform us rebase is in progress.
- Resolve conflict
- save file
git add .
git commit (Don’t do commit!)
git rebase —-continue
- You can see first commit message.
:wq
- repeat 1-5 until rebase finished
If you can't resolve conflict, rollback it git rebase --abort
4. Last step of rebase
- Finally, write the commit messsage of this rebased commit.
- It shows all the commit messages that we've written so far.
- Clean up the unnecessory messages and write the commit message about the rebase
:wq
5. Successfully rebased !
Check commit message usning git log
6. Push after rebase
git push origin [branch] -f
(force push)
- Rebase cleans up the commit history
- Commit history can be changed when we rebase on the same branch
- Commit history is changed when we modify code and rebase it
- git does not allow pushing of branches with different histories.