Sunday, May 27, 2018

Git Merge Vs Rebase

I have used the below steps to test Git Merge and Git Rebase.

1) Init Master,
2) Init Branch.
3) Commit "1", in Branch. . 
4) Commit "2"  in Master. 
5) Commit "3" in Branch. 

Git Merge:  

"Your repository’s commit history is a record of what actually happened."

git checkout mybranch
git merge master 


Note in the above diagram, I have merge the master into branch.


Note that, the second commit is applied after the third commit in my branch, and a new merge commit is created.


Git Rebase:  

"The repository's commit history is the story of how your project was made."

git checkout mybranch
git rebase master. 

Here I have rebased mybranch onto master

Now here instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.




See that all the branch history is applied on top of master log history.


Way to use

In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere.

No comments:

Post a Comment