git rebase vs merge
DESCRIPTION
Introduction to Merge and Rebase concepts in Git, Pros and Cons and more.TRANSCRIPT
![Page 1: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/1.jpg)
Git Rebase vs Mergea deep dive into the mysteries of revision control
![Page 2: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/2.jpg)
Merge
![Page 3: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/3.jpg)
Merge
a new commit on top of both branches that should be merged - known as merge commit
![Page 4: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/4.jpg)
Merge
You work on feature_branch
![Page 5: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/5.jpg)
MergeBefore pushing you update your branch with master’s changes - git pull origin master
![Page 6: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/7.jpg)
Merge history graph
![Page 8: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/9.jpg)
Rebasea different approach
![Page 10: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/11.jpg)
Rebase
Let’s get back to our previous example:
![Page 12: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/16.jpg)
Rebase
git rebase [branch]
![Page 17: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/19.jpg)
Rebase history graph
![Page 20: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/21.jpg)
The key of having a clean history
… is to have a “fast forward” effect
![Page 22: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/22.jpg)
Fast Forward
![Page 23: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/24.jpg)
Who uses what?
Merge➔ Atlassian (with GitHub pull requests for code reviews)➔ Git
Rebase➔ Guava
![Page 25: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/25.jpg)
Rebase
If you are not sure you fully understand rebase -
Don’t do it…
BUT
![Page 26: Git Rebase vs Merge](https://reader033.vdocuments.site/reader033/viewer/2022052504/554a50c2b4c9054b328b47aa/html5/thumbnails/26.jpg)
If you like it, try out !!!