This is a quick post to remind myself the differences between git merge and rebase.

Merge Link to heading

Let’s start with merge. merge keeps branch history across branches. Considering a scenario where feature1 branch is 2 commits ahead of master.

* f77205c (HEAD -> master) repo 1
| * 1656afc (feature1) feature1
| * dc14156 feature1
|/  
* cb3e096 repo 1
* a387011 repo 1

Merging master to feature1 branch creates a commit on feature1 branch.

git switch feature1
git merge master
*   4426edc (HEAD -> feature1) Merge branch 'master' into feature1
|\  
| * f77205c (master) repo 1
* | 1656afc feature1
* | dc14156 feature1
|/  
* cb3e096 repo 1
* a387011 repo 1

If there are conflict, it has to be resolved manually. Then files are added to be staged.

Rebase Link to heading

Now we have the scenario again where feature1 branch is ahead and we need to bring in stuff from master.

* addc96f (HEAD -> feature1) feature1
* da4970a feature1
*   4426edc Merge branch 'master' into feature1
|\  
* | 1656afc feature1
* | dc14156 feature1
| | * b74909f (master) repo 1
| |/  
| * f77205c repo 1
|/  
* cb3e096 repo 1
* a387011 repo 1

This time, we use rebase to bring in master to feature1

git switch feature1
git rebase master
Auto-merging README
CONFLICT (content): Merge conflict in README
error: could not apply dc14156... feature1
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply dc14156... feature1 

Fix the merge issue and git add, then continue the rebase

git rebase --continue

We see that feature1 branch essentially starts from HEAD of master.

* 4dccc50 (HEAD -> feature1) feature1
* c7efead feature1
* 724d30c feature1
* eb39185 feature1
* b74909f (master) repo 1
* f77205c repo 1
* cb3e096 repo 1
* a387011 repo 1