When do you use git rebase instead of git merge?

2018-12-31 02:15发布

When is it recommended to use git rebase vs. git merge?

Do I still need to merge after a successful rebase?

14条回答
妖精总统
2楼-- · 2018-12-31 02:59

When do I use git rebase? Almost never, because it rewrites history. git merge is almost always the preferable choice, because it respects what actually happened in your project.

查看更多
柔情千种
3楼-- · 2018-12-31 03:01

It's simple, with rebase you say to use another branch as the new base for your work.

If you have for example a branch master and you create a branch to implement a new feature, say you name it cool-feature, of course the master branch is the base for your new feature.

Now at a certain point you want to add the new feature you implemented in the master branch. You could just switch to master and merge the cool-feature branch:

$ git checkout master
$ git merge cool-feature

but this way a new dummy commit is added, if you want to avoid spaghetti-history you can rebase:

$ git checkout cool-feature
$ git rebase master

and then merge it in master:

$ git checkout master
$ git merge cool-feature

This time, since the topic branch has the same commits of master plus the commits with the new feature, the merge will be just a fast-forward.

查看更多
登录 后发表回答