Git Rebase vs Merge

Preview:

DESCRIPTION

Introduction to Merge and Rebase concepts in Git, Pros and Cons and more.

Citation preview

Git Rebase vs Mergea deep dive into the mysteries of revision control

Merge

Merge

a new commit on top of both branches that should be merged - known as merge commit

Merge

You work on feature_branch

MergeBefore pushing you update your branch with master’s changes - git pull origin master

Merge

git pull = git fetch + git merge

git fetch - copies origin master into origin/mastergit merge - merges origin/master into master

Merge history graph

Merge - Pros & Cons

Pros:● Simple to use and understand.● Keeps information about the historical existence of branches.● Existing commits on the source branch are unchanged and remain valid.

Cons:● History can become intensively polluted by lots of merge commits.● Visual charts of your repository can have non-informative branch lines.

Rebasea different approach

Rebase

a way to cut of a set of commits from a branch and apply those commits on another branch

Rebase

Let’s get back to our previous example:

Rebase

What does rebase do: It cuts off these commits

The commits don’t have any information about their parents anymore.

Rebase

The system then applies them on top of the new branch.

We literally cut of these commits and then apply it on top of the new branch.

Rebase

Why does merge even exists if we found such a nice way to handle our history?

Our commit IDs changed!

Why?

Rebase

...because we have a new parent.

Git thinks of our commits as patches and applies them on top of the new branch. Therefore Git processes our commits as new commits.

And again, NEW COMMITS!

Rebase

git rebase [branch]

Rebase - Golden Rule

● Never rebase commits that you have pushed to a public repository. Only rebase local branches.

Why?

Rebase - Golden Rule

● When you rebase pushed branch, you’re abandoning existing commits and creating new ones that are similar but different.

So you will rewrite the history...

Rebase history graph

Rebase - Pros & Cons

Pros:● Clean and flat history. ● Concise commits covering a logical change (can squash series of commits

into one)● Reduction of merge commits● Manipulating single commit is easy (e.g. reverting a commit)

Cons:● More complex● Rebasing can be dangerous! Can cause history rewrite if done incorrectly

The key of having a clean history

… is to have a “fast forward” effect

Fast Forward

Rebase - Best Practice

$ git checkout -b security_hole_fix...fix...fix...fix$ git commit $ git rebase master$ git checkout master$ git merge security_hole_fix

then you get fast-forward effect

Who uses what?

Merge➔ Atlassian (with GitHub pull requests for code reviews)➔ Git

Rebase➔ Guava

Rebase

If you are not sure you fully understand rebase -

Don’t do it…

BUT

If you like it, try out !!!

Recommended