When do you use Git rebase instead of Git merge?

后端 未结 17 2390
独厮守ぢ
独厮守ぢ 2020-11-21 11:25

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

Do I still need to merge after a successful rebase?

17条回答
  •  不思量自难忘°
    2020-11-21 12:11

    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, you create a branch to implement a new feature, and 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.

提交回复
热议问题