When do you use Git rebase instead of Git merge?

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

    While merging is definitely the easiest and most common way to integrate changes, it's not the only one: Rebase is an alternative means of integration.

    Understanding Merge a Little Better

    When Git performs a merge, it looks for three commits:

    • (1) Common ancestor commit. If you follow the history of two branches in a project, they always have at least one commit in common: at this point in time, both branches had the same content and then evolved differently.
    • (2) + (3) Endpoints of each branch. The goal of an integration is to combine the current states of two branches. Therefore, their respective latest revisions are of special interest. Combining these three commits will result in the integration we're aiming for.

    Fast-Forward or Merge Commit

    In very simple cases, one of the two branches doesn't have any new commits since the branching happened - its latest commit is still the common ancestor.

    In this case, performing the integration is dead simple: Git can just add all the commits of the other branch on top of the common ancestor commit. In Git, this simplest form of integration is called a "fast-forward" merge. Both branches then share the exact same history.

    In a lot of cases, however, both branches moved forward individually.

    To make an integration, Git will have to create a new commit that contains the differences between them - the merge commit.

    Human Commits & Merge Commits

    Normally, a commit is carefully created by a human being. It's a meaningful unit that wraps only related changes and annotates them with a comment.

    A merge commit is a bit different: instead of being created by a developer, it gets created automatically by Git. And instead of wrapping a set of related changes, its purpose is to connect two branches, just like a knot. If you want to understand a merge operation later, you need to take a look at the history of both branches and the corresponding commit graph.

    Integrating with Rebase

    Some people prefer to go without such automatic merge commits. Instead, they want the project's history to look as if it had evolved in a single, straight line. No indication remains that it had been split into multiple branches at some point.

    Let's walk through a rebase operation step by step. The scenario is the same as in the previous examples: we want to integrate the changes from branch-B into branch-A, but now by using rebase.

    We will do this in three steps

    1. git rebase branch-A // Synchronises the history with branch-A
    2. git checkout branch-A // Change the current branch to branch-A
    3. git merge branch-B // Merge/take the changes from branch-B to branch-A

    First, Git will "undo" all commits on branch-A that happened after the lines began to branch out (after the common ancestor commit). However, of course, it won't discard them: instead you can think of those commits as being "saved away temporarily".

    Next, it applies the commits from branch-B that we want to integrate. At this point, both branches look exactly the same.

    In the final step, the new commits on branch-A are now reapplied - but on a new position, on top of the integrated commits from branch-B (they are re-based).

    The result looks like development had happened in a straight line. Instead of a merge commit that contains all the combined changes, the original commit structure was preserved.

    Finally, you get a clean branch branch-A with no unwanted and auto generated commits.

    Note: Taken from the awesome post by git-tower. The disadvantages of rebase is also a good read in the same post.

提交回复
热议问题