Git - Git Merge VS Git Rebase

Article directory

Insert image description here


Overview

Git merge and Git rebase are two different version control workflows that are used to merge changes from one branch to another. They have different working principles and application scenarios. The following are their main differences:

  1. How to merge:

    • Git Merge: The merge operation creates a new merge commit that merges the changes from the two branches. This merge commit has two parent commits, one from the current branch and one from the branch being merged. This preserves the full history of the branch, but may cause the branch history to become cluttered.
    • Git Rebase: The rewrite history operation moves the commits of the current branch after the latest commits of the target branch and reapplies those commits. This will look like it is part of a continuous commit on the target branch and will not create a merge commit. This keeps the branch history linear and makes the history clearer.
  2. Clarity of history:

    • Git Merge: Merge commits preserve the complete history of a branch, but may introduce redundant merge commits into the branch history, complicating the history.
    • Git Rebase: Rewriting history can make branch history clearer because it linearly arranges commits together without introducing additional merge commits. But this may also result in loss of information because the original branch's commit ID changes.
  3. Handling merge conflicts:

    • Git Merge: If a conflict occurs during the merge process, Git will create the merge conflict and wait for the user to resolve it manually. After resolution, the user commits the merge conflicting changes and the merge continues.
    • Git Rebase: If a conflict occurs while rewriting history, Git will pause at each conflict point and wait for the user to resolve the conflict. The user then submits a resolution to the conflict, and history continues to be rewritten. This may require more interaction.
  4. scenes to be used:

    • Git Merge: Usually used to merge public branches (such as the master branch) into feature branches or merge multiple parallel development feature branches into the master branch. It keeps a complete history of the branch and helps track the branch's evolution.
    • Git Rebase: Typically used to rearrange commits on a local branch to keep branch history linear so that it remains clear when merging. It can also be used to keep your own branch in sync with the target branch to make merging easier.

Flow View

Insert image description here

summary

In conclusion, both Git Merge and Git Rebase have their uses, depending on the needs of the project and the workflow of the team. Which method you choose depends on whether you are more concerned with preserving a complete history or maintaining clarity.

Best Practices

Most companies actually disable rebase, and use merge regardless of whether they are pulling code or pushing code. Although there will be a meaningless submission record "Merge... to...", at least you can clearly know who merged on the main line. Code and the chronological order in which they are combined into the code

Insert image description here

Guess you like

Origin blog.csdn.net/yangshangwei/article/details/132834388