When do you use Git rebase instead of Git merge?

后端 未结 17 2364
独厮守ぢ
独厮守ぢ 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:26

    It was explained many times what rebase and what merge is, but when should you use what?

    When should you use rebase?

    Rebase "lifts off" your changes and puts all the changes of the rebased branch into your current branch and then puts your changes on top of it. It therefore changes the history of your branch.

    • when you have not pushed the branch / no one else is working on it
    • you want your to see all your changes at one point together when merging back to the source branch
    • you want to avoid the auto-generated "merged .." commit messages

    I said "you want to see all your changes at one place" because sometimes a merge operation puts all your changes together in one commit (some: merged from ... message). Rebase makes your change look like you made your commits all after each other with no one else doing something in between. This makes it easier to see, what you changed for your feature.

    Make sure though, you use git merge feature-branch --ff-only to make sure there are no conflicts creating a single commit when you are merging your feature back to develop/master.

    When should you use merge?

    • when you have pushed the branch / others are working on it too (rebase gets very complicated if others work on that branch too!)
    • you don't need the full history(*) / your feature doesn't have to have it's commits all in one place.

    (*) you can avoid that your feature only gets one "merged .." commits by first merging the develop branch to your feature and then merging your feature back to develeop. This still gives you a "merged .." commit, but a least all the commits of your feature are still visible.

提交回复
热议问题