Git Merges
Merge vs Rebase
Great explanation from this tutorial and this video
1 Merge
git merge main
is a non-destructive operation. The existing branches are not changed in any way, and feature branch will have an extraneous merge commit every time you need to incorporate upstream changes.
It’s a good pratice to merge changes from main and eventually do a PR to when pushing the feature to main
git push origin feature
2 Fast-Forward Merge
When there is no new commit in the main branch, mering a feature branch would lead to fast-forward merge.
git merge feature
2 Rebase
When there is new commit in main and it’s forked situation. FF is not feasible and a rebase or 3 way merge is needed
git rebase main
moves the entire feature branch to begin on the tip of the main branch, effectively incorporating all of the new commits in main. It re-writes the project history.
Use git rebase --continue
after solve the conflicts. and staging them after the changes. or --abort
to quit
Use git rebase --interactive HEAD~3
to interactively rebase previous 3 commits.
Now you can fast-forwrd merge the feature branch in main.(or squash and merge, which squash multipl commits into one)
3 The golden rule of rebasing
Never use it on public branch. It will force main to get all commit from you, while others don’t need.
Never use it on already pushed commits.