Git Merge vs Rebase in Examples. All you need to know.

Image for post
Image for post

Branch — is just a reference to a commit.

git cat-file <SHA> -t // type
git cat-file <SHA> -p // pretty printing
Image for post
Image for post
Image for post
Image for post
p —> parent
git branch
Image for post
Image for post
git branch third
Image for post
Image for post
Image for post
Image for post

HEAD — is just a reference to a branch, a pointer to a pointer

Image for post
Image for post
Image for post
Image for post

GIT MERGE

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

1. Abort merge action

git merge --abort
Image for post
Image for post

2. Solve conflicts and Merge

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Merge is a simple commit with one exeption — it has two parent

Image for post
Image for post
Image for post
Image for post

Fast-forward merge

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

HEAD pointing not to branch, to commit → Detached HEAD

Three rules:

GIT REBASE

Image for post
Image for post
Image for post
Image for post

What is happing under the hood?

“GIT object is literally just specific contents of the file…Objects are immutable. “

- git documentation

Example:

Image for post
Image for post

Commit with id “3” will be garbage-collected by GIT, soon or later. Git garbage-collects unreachable objects.

Rebase creates new commits and leaves behind existing commits that might get garbage collected

So a rebase history looks cleaner, but it is a lie, in its own way.

Image for post
Image for post

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store