When do you use Git rebase instead of Git merge?

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

    I just created a FAQ for my team in my own words which answers this question. Let me share:

    What is a merge?

    A commit, that combines all changes of a different branch into the current.

    What is a rebase?

    Re-comitting all commits of the current branch onto a different base commit.

    What are the main differences between merge and rebase?

    1. merge executes only one new commit. rebase typically executes multiple (number of commits in current branch).
    2. merge produces a new generated commit (the so called merge-commit). rebase only moves existing commits.

    In which situations should we use a merge?

    Use merge whenever you want to add changes of a branched out branch back into the base branch.

    Typically, you do this by clicking the "Merge" button on Pull/Merge Requests, e.g. on GitHub.

    In which situations should we use a rebase?

    Use rebase whenever you want to add changes of a base branch back to a branched out branch.

    Typically, you do this in feature branches whenever there's a change in the main branch.

    Why not use merge to merge changes from the base branch into a feature branch?

    1. The git history will include many unnecessary merge commits. If multiple merges were needed in a feature branch, then the feature branch might even hold more merge commits than actual commits!

    2. This creates a loop which destroys the mental model that Git was designed by which causes troubles in any visualization of the Git history.

      Imagine there's a river (e.g. the "Nile"). Water is flowing in one direction (direction of time in Git history). Now and then, imagine there's a branch to that river and suppose most of those branches merge back into the river. That's what the flow of a river might look like naturally. It makes sense.

      But then imagine there's a small branch of that river. Then, for some reason, the river merges into the branch and the branch continues from there. The river has now technically disappeared, it's now in the branch. But then, somehow magically, that branch is merged back into the river. Which river you ask? I don't know. The river should actually be in the branch now, but somehow it still continues to exist and I can merge the branch back into the river. So, the river is in the river. Kind of doesn't make sense.

      This is exactly what happens when you merge the base branch into a feature branch and then when the feature branch is done, you merge that back into the base branch again. The mental model is broken. And because of that, you end up with a branch visualization that's not very helpful.

    Example Git History when using merge:

    Note the many commits starting with Merge branch 'develop' into .... They don't even exist if you rebase (there, you will only have pull request merge commits). Also many visual branch merge loops (develop into feature into develop).

    Example Git History when using rebase:

    Much cleaner Git history with much less merge commits and no cluttered visual branch merge loops whatsoever.

    Are there any downsides / pitfalls with rebase?

    Yes:

    1. Because a rebase moves commits (technically re-executes them), the commit date of all moved commits will be the time of the rebase and the git history loses the initial commit time. So, if the exact date of a commit is needed for some reason, then merge is the better option. But typically, a clean git history is much more useful than exact commit dates.
    2. If the rebased branch has multiple commits that change the same line and that line was also changed in the base branch, you might need to solve merge conflicts for that same line multiple times, which you never need to do when merging. So, on average, there's more merge conflicts to solve.

    Tips to reduce merge conflicts when using rebase:

    1. Rebase often. I typically recommend doing it at least once a day.
    2. Try to squash changes on the same line into one commit as much as possible.

提交回复
热议问题