Git Rebase vs Merge

26
Git Rebase vs Merge a deep dive into the mysteries of revision control

description

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

Transcript of Git Rebase vs Merge

Page 1: Git Rebase vs Merge

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

Page 2: Git Rebase vs Merge

Merge

Page 3: Git Rebase vs Merge

Merge

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

Page 4: Git Rebase vs Merge

Merge

You work on feature_branch

Page 5: Git Rebase vs Merge

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

Page 6: Git Rebase vs Merge

Merge

git pull = git fetch + git merge

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

Page 7: Git Rebase vs Merge

Merge history graph

Page 8: Git Rebase vs Merge

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.

Page 9: Git Rebase vs Merge

Rebasea different approach

Page 10: Git Rebase vs Merge

Rebase

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

Page 11: Git Rebase vs Merge

Rebase

Let’s get back to our previous example:

Page 12: Git Rebase vs Merge

Rebase

What does rebase do: It cuts off these commits

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

Page 13: Git Rebase vs Merge

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.

Page 14: Git Rebase vs Merge

Rebase

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

Our commit IDs changed!

Why?

Page 15: Git Rebase vs Merge

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!

Page 16: Git Rebase vs Merge

Rebase

git rebase [branch]

Page 17: Git Rebase vs Merge

Rebase - Golden Rule

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

Why?

Page 18: Git Rebase vs Merge

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

Page 19: Git Rebase vs Merge

Rebase history graph

Page 20: Git Rebase vs Merge

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

Page 21: Git Rebase vs Merge

The key of having a clean history

… is to have a “fast forward” effect

Page 22: Git Rebase vs Merge

Fast Forward

Page 23: Git Rebase vs Merge

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

Page 24: Git Rebase vs Merge

Who uses what?

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

Rebase➔ Guava

Page 25: Git Rebase vs Merge

Rebase

If you are not sure you fully understand rebase -

Don’t do it…

BUT

Page 26: Git Rebase vs Merge

If you like it, try out !!!