Git for solo developer: merge vs rebase
This question already has an answer here:
merge does as the name says, it combines two branches of development into one. So imagine you have a master branch at commit M1. Then you work on your notebook and create commit N. And you also work on your desktop and create commit L:
N
/
M1-L
When you merge N into L you combine the changes and get new commit M2
N
/
M1-L-M2
This keeps all the changes as they where made, but you get this little double paths, which can become pretty confusing, especially when you have lots of them.
Then there is rebase. Starting from the same situation a rebase takes one of the commit N or L and pretends it was made after the other. This results in a new commit n'
M1-L-N'
N' and M2 have the same content, just their history looks different. As long as you are alone, it doesn't matter. You'll have a current state both ways
If you are in a team the difference becomes important:
When you rebase a branch that got already pulled by somebody else he might see some confusing effects since the branch he was working on suddenly contains completely different commits. So you don't want to rebase stuff that went public.
On the other hand if you have ten developers commiting and merging like hell the history becomes confusing and you might prefer to have developers work on private repositories and then rebase before pushing to the integration branch.
rebase and merge are different kind of strategies for bringing your branch uptodate with another branch. rebase changes the history to the point where both branches started diverging. merge on the other hand, does not alter history and might create a new commit to show the merge of two branches.
See also this document on rebase vs merge. Bottomline: use rebase for unpushed changes to create neat history; otherwise use merge.
Update: Linus wrote about this in 2009, see his advice on git rebase and merge. Bottom line: rebase only your own private stuff, with caution. If your commits have been pushed, no more rebasing.
链接地址: http://www.djcxy.com/p/49266.html