Git Merges

1 minute read

Merge vs Rebase

Great explanation from this tutorial and this video

1 Merge

git merge mainis 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.
Alt text

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 Alt text

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 Alt text Alt text

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.
Alt text

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) Alt text

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. Alt text

Never use it on already pushed commits.

Tags:

Categories:

Updated: