Friday, November 10, 2017

git merge and rebase from same and forked repository

Git Merge and rebase from same repository
Both merge and rebase can be used to integrate changes from one branch to another, however they use different approach. Consider the below case:
A forked commit history



git merge
As the name imply, when git merge is used as below
git checkout A
git merge B
(git pull B)
It appends all the change from B as a single commit to the current history of A. So all changes happened in B are added as a single new changes in A. None of the existing commit history in branch A is affected by this.
Merging master into the feature branch





git rebase
As an alternative to merge,
git checkout A
git rebase B
(git pull --rebase B)
Rebase will insert each commit happened in B into the history of A, so the changes in B get inserted at the proper history in A Rebasing the feature branch onto master



Git merge for forked repository
If you have a forked repository, usually you will make some change and then send a pull request to update the original repository. But quite often you will need to update your forked repository to bring back the change made in original master repository. Although you can do this from git bash command, but if you prefer there is an easier way to do this from github web page.

In the forked repository, click the pull request button, in the next page, click "switching the base" button as you want to merge the change from original repository to your forked repository. Then create the pull request and merge the pull request.



No comments:

Post a Comment