Git rebase VS git merge

Jimmy (xiaoke) Shen
1 min readAug 26, 2020

--

If you have your own branch and the master branch is updated. How to update the master branch’s update to your own branch?

Git merge

git checkout -b newbranch #use this one if you don't have the newbranch before
git checkout newbranch
git merge origin/master
git push origin newbranch

Git rebase

git checkout newbranch
git fetch
git rebase origin/master
git push origin newbranch

If you have conflict after rebase, you need resolve those conflicts manually on terminal one by one. After that, when you type

git rebase --continue

And there is no error message, you can run the following to force push

git push -f origin <you_branch_name>

The difference between merge and rebase

It can be shown from the nice illustration below[2].

Image source [2]

Rebase master and force push to feature branch

A nice discussion can be found in [3].

Reference

[1]https://intellipaat.com/community/11548/update-git-branches-from-master-git-update-branch-from-master

[2]https://medium.com/datadriveninvestor/git-rebase-vs-merge-cc5199edd77c

[3] https://stackoverflow.com/questions/8939977/git-push-rejected-after-feature-branch-rebase#:~:text=If%20you%20rebase%20a%20branch,rebase%20will%20be%20quite%20unpleasant.

--

--

No responses yet