Home  >  Article  >  Development Tools  >  What is merge in git

What is merge in git

青灯夜游
青灯夜游Original
2021-12-29 17:41:3619052browse

In git, merge means "merge". This command is used to merge two or more development histories together; the merge command can be used in git-pull to integrate Changes in another repository; can also be used for merging from one branch to another.

What is merge in git

The operating environment of this tutorial: Windows 7 system, Git version 2.30.0, Dell G3 computer.

git-merge complete analysis

Git's git-merge is a command frequently used in Git. Many people think that git merge is a very troublesome thing. If you are careful, you will encounter the problem of losing code, which will make you shy away from git. This article provides a complete and detailed introduction to the git-merge command based on Git 2.8.2, especially regarding the code loss problem caused by cross-merging. I give my own suggestions at the end of the article, hoping to help git users. The content introduced in this article is based on Git 2.8.2

The git-merge command is used to merge two or more development histories together. It can also usually be written as: git merge.

1.git-merge related option parameters

1.1 Summary

In the git-merge command, there are the following three parameters:

  • git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit] [-s <strategy>] [-X ] [-S[<keyid>]] [--[no-]rerere-autoupdate] [-m <msg>] [<commit>...]</commit></msg></keyid></strategy>
  • git merge <msg> HEAD <commit>...</commit></msg>
  • git merge --abort
##1.2git Introduction to -merge

The git-merge command is used to merge from specified commit(s) to the current branch.

Note: The specified commit(s) here refers to starting from these historical commit nodes until the current separation.

The git-merge command has the following two uses:

    is used in git-pull to integrate changes in another code repository (ie: git pull = git fetch git merge)
  1. For merging from one branch to another branch
Assume that the following historical node exists and the current branch is "master":


What is merge in git

Then the

git merge topic command will merge the node (i.e. topic) separated after the common node (E node) on the master branch. The A B C node of the branch) reappears on the master branch until the current commit node of the topic branch (C node), and is located on the top of the master branch. And create a new node along the master branch and topic branch to record the merge results. This node carries the user's information describing the merge changes.

That is, the H node in the figure below, the C node and the G node are both the parent nodes of the H node.

What is merge in git##1.3

git merge HEAD ...

CommandExistence of this command Due to historical reasons, it should not be used in the new version.

git merge -m ....

should be used instead1.4

git merge --abort

CommandThis command is only used when conflicts result after merging.

git merge --abort

will abandon the merge process and try to rebuild the pre-merge state. However, if there are uncommitted files when the merge starts, git merge --abort will not be able to reproduce the pre-merge state in some cases. (Especially when these uncommitted files will be modified during the merge process)

Warning: It is easy to contain a large number of uncommitted files when running
git-merge

You get stuck in a situation that will make it difficult for you to back down in a conflict. Therefore, it is highly discouraged to have uncommitted files when using git-merge. It is recommended to use the git-stash command to temporarily store these uncommitted files and use them after resolving conflictsgit stash popRestore these uncommitted files.

2. Parameters

This section is used to introduce the parameters used in the

git-merge

command2.1

--commit The

and --no-commit

--commit

parameters cause a commit node of the merged result to be generated after merging. This parameter can override --no-commit. The --no-commit
parameter causes the merge to not be automatically submitted in order to prevent the merge from failing, giving the user a chance to review and modify the merge result before submission. 2.2

--edit

and -e and --no-edit

--edit

and -e are used to call the editor to further edit the automatically generated merge information before successful merge and submission. Therefore, users can further interpret and judge the results of the merger. The --no-edit
parameter can be used to accept automatically merged information (this is generally discouraged). <blockquote><p>If you have given the <code>-m parameter when merging (described below), use --edit (or -e) still is useful, this will further edit the content contained in -m in the editor.

Older versions of nodes may not allow users to edit merge log information.

2.3--ffCommand

--ff refers to the fast-forward command. When merging using fast-forward mode, a new commit node will not be created. By default, git-merge adopts fast-forward mode.
For a detailed explanation of the fast-forward mode, please see my other article: "About fast forward" section of a successful Git branch model.

2.4--no-ffCommand

Create a new merge node even if fast-forward mode can be used. This is the default behavior when git merge merges a tag.

2.5--ff-onlyCommand

Unless the current HEAD node has been up-to-date (update points to the latest node) or fast-forward mode can be used Merge, otherwise the merge will be rejected and a failure status will be returned.

2.5 --log[=<n>]</n> and --no-log

--log[=&lt ;n>] When merging and submitting, in addition to the branch name, it will also contain the log information of up to n merged commit nodes.
--no-log will not list this information.

2.6 --stat, -n, --no-statCommand

--stat The parameter will display the status of file differences at the end of the merge result. The status of file differences can also be configured in merge.stat in the git configuration file.
In contrast, the -n, --no-stat parameters will not display this information.

2.7--squash and --no-squash

--squash When a merge occurs, from The other branch node after the common ancestor node of the current branch and the other branch until the top node of the other branch will be compressed together. The user can submit it after review to generate a new node.

Note 1: This parameter conflicts with --no-ff

##Note 2: The result after using this parameter is similar to Commit a new node on the current branch. This parameter is very useful in some cases, such as when using Git Flow (about Git Flow, please refer to: A successful Git branch model). When the function branch is developing a functional requirement, the developer may submit a large number of locally. And meaningless nodes, when they need to be merged into the develop branch, you may only need to use a new node to represent the modification content of this long list of nodes. At this time, the

--squash command will come into play. In addition, if multiple submissions of the feature branch are not trivial but are meaningful, using the --no-ff command is more appropriate.
--no-squash has the exact opposite effect.

2.8

-s and --strategy=

##-s

and --strategy=<strategy></strategy> are used to specify the merge strategy. By default, if this parameter is not specified, git will use the default merge strategy according to the following conditions:

When the merge node only contains a single parent node (such as when using fast-forward mode), the recursive strategy will be used ( introduced below).
  1. When the merged node contains multiple parent nodes (such as when using no-fast-forward mode), the octopus strategy (described below) is used.
  2. 2.9
-X

The above is the detailed content of What is merge in git. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn