Hongmu Notes

Git merge command

Merging connects the history of two branches together; whether there is a fork between the two branches determines whether to generate a merge node.
100% Browser local processing 0 Data upload

🌿 Command assembly

💡 Instructions for use

  • The branch name and remote name must be written correctly. Misspelled commands often do not report an error but take effect on other branches.
  • Commands involving reset, force push, and rebase will rewrite history. Please confirm the branch and target before execution.
  • Check Bind Upstream when pushing a new branch for the first time. You can omit the remote and branch names for subsequent pushes and pulls.
  • The command is output in a single line, making it easy to paste directly into the terminal; it is recommended to save the steps that need to be repeated into a script.

Merging connects the history of two branches together; whether there is a fork between the two branches determines whether to generate a merge node.

Fast forward and merge nodes

If there are no new commits on the target branch since you forked, the merge only needs to move the pointer forward. This process is called fast forward and no additional nodes will be generated. If both sides have new submissions, a merge node is needed to connect the two histories. At this time, a merge record will be generated and the merge message may be filled in. Only by understanding the difference between the two can we predict what the post-merger history will look like.

Why is it sometimes necessary to disable fast forwarding?

Disabling fast forwarding forces the generation of merge nodes even though fast forwarding would be possible. The advantage of this is that you can clearly see "a feature branch has been merged here" in the history, which makes it easier to trace later when and which batch of submissions a certain feature was merged into the trunk. This option is usually added when the team agrees to collaborate by branching and merging.

How to deal with conflicts

  • Conflicts only occur when both sides change the same content, and the tool will mark both pieces of content.
  • Decide on a piecemeal basis which side to keep, or manually integrate the two pieces into the final version.
  • After solving the problem, add the file to the temporary storage area, then submit it, and the merge will be completed.

If you don’t want to continue halfway, you can give up the merge, and the warehouse will return to the state before the merge, leaving no semi-finished products.

Pre-merger checks

Before merging, make sure that the current branch is the target branch to receive changes. Writing the wrong branch will merge changes into places they shouldn't go. Secondly, deal with the uncommitted changes in the workspace, otherwise it will be difficult to distinguish which are the original changes when conflicts occur. Finally, confirm that the name of the branch to be merged is accurate. If necessary, first look at the additional commits on both sides.

Before merging, confirm that the current branch is the recipient. Otherwise, writing to the target branch will merge the changes in the wrong place.

📖 Glossary

workspace
The directory where the file being edited is located. The modified content that has not yet been registered only exists in the workspace and may be overwritten by any rollback, so important changes must be submitted first.
Staging area
A preparation area between the work area and the warehouse. Submissions only record the contents of the staging area, so a batch of changes can be split into several submissions, or only part of them can be submitted.
Submit
Solidify the contents of the temporary storage area into a historical node, with description, author and time. Once a submission is generated, it has a unique number, which is used as the minimum unit for rollback and tracing.
branch
A movable pointer to a commit, used for parallel development of changes that do not affect each other. A new branch takes up almost no space. Switching branches means that the work area and temporary storage area follow the pointer.
head pointer
A pointer that marks the current location, usually pointing to a branch. If it points directly to a commit and is not on a branch, it is a detached head state, and the new commit at this time will not update any branches.
merge
Merge the history of another branch into the current branch to generate a new merge node. If the two branches do not diverge, they will be fast forwarded directly and no merge node will be generated.
Rebase
Move a series of commits on the current branch to another branch and replay, so the history becomes a straight line. Do not rebase the submitted commits that have been pushed out, otherwise the local history of the collaborators will be inconsistent.
reset
Move the current branch pointer to the specified commit. The three modes respectively determine whether to modify the staging area and the work area at the same time. The mode of changing the work area will directly discard uncommitted modifications.
Remote warehouse
A copy of the same repository located on the server, referenced locally by a name. You can change the address or name of the same remote. Both push and pull are addressed by name.
upstream branch
Establish a corresponding relationship between the local branch and the remote branch. You can omit the remote name and branch name when pushing and pulling after binding, and you can also directly see how many commits are ahead or behind.
label
A fixed name given to a commit, often used to mark versions. Tags with descriptions will generate an additional object containing the author and time. This is recommended for release versions.
storage
Put away unfinished changes temporarily to return the workspace to a clean state and retrieve them when needed. It is suitable for situations where you are called to fix an urgent problem midway but do not want to submit it hastily.

🔗 All keywords and tutorials

Keywords page of the tool12a

Return to tool home page

❓ FAQ

Can it be revoked after merging??
Yes. As long as the merged node has not been pushed, the branch pointer can be moved back to the position before the merge, and the operation is reversible.
Is it normal to have a lot of conflicts??
The longer the fork is and the more concentrated the changes are, the more conflicts there will be. Frequently synchronizing with the trunk and narrowing the scope of each change can significantly reduce conflicts.