Home  >  Article  >  Development Tools  >  How to merge branch code with git

How to merge branch code with git

青灯夜游
青灯夜游Original
2023-01-04 11:24:4810311browse

How to merge branch code with git: 1. Use the "git merge" command. This command is used to merge branches. It can merge the contents of other branches into the current branch. 2. Use the "git rebase" command, which is used to change the base point of the current branch to achieve branch merging.

How to merge branch code with git

## Operating environment for this tutorial: Windows 7 system, Git2.30.0 version, Dell G3 computer.

In the project, we always create many branches to develop different functions or requirements, and then merge them back into the main branch after the functions are completed. So how can you merge branches gracefully? If you are interested at this point, you might as well continue reading.

Establishing a multiplayer development scenario

1. Create a warehouse
// 初始化仓库
git init
// 创建a.txt
touch a.txt
// 创建b.txt
touch b.txt
// 加入暂存区
git add .
// 提交
git  commit -m 'initial'

How to merge branch code with git

2. Create feature branch
git checkout -b feature

How to merge branch code with git

3. Develop two branches at the same time

Feature branch develops the next one The new features of the version were submitted twice, and the a.txt file and b.txt file were modified respectively.


How to merge branch code with git

The master branch developed the function of this version and submitted it twice, and modified the a.txt file and b.txt file.


How to merge branch code with git

The current branch situation is as shown below. The characters on each node are the hash values ​​of each commit. The header of the current master branch is on the c5 node, and the feature branch The header is on the c3 node.


How to merge branch code with git

At this time, you need to merge the feature branch back to the master branch. There are two options:

  • Merge the feature branch directly on the master branch;

  • is to rebase on the feature branch first, and then merge the feature branch on the master branch.

The following are the two options:

Merge command 1: git merge

git merge is used to merge branches and merge content from other branches into the current branch.

The git merge operation is relatively violent and is also a commonly used method. The following demonstration is to merge the feature branch into the master branch. The specific process is as follows:

  • Find the feature branch Merge the latest commit node c3 of the feature branch with the latest commit node c5 of the master branch. If there is a conflict at this time, Then resolve all conflicts at once, and then generate a new commit node c6;

  • At the same time, according to the order of commit time on the two branches, put them on the master branch in turn, use git Log can see the chronological order.

  • The result diagram of the above process is as follows:

The operation commands in the project are as follows. You can see that after executing the

git merge feature

command, there are conflicts, enter the merging workspace, and then resolve all conflicts at once and submit a new commit. How to merge branch code with git

Execute the gitk command line, and you can see the current branch on the interface as shown below. There is a new commit.

How to merge branch code with git


How to merge branch code with git

Merge command 2: git rebaseThis command is intuitive from the name See its function: change the base point

of the current branch. For the feature branch, it is a branch created from the c1 node of the master branch, so its

base point

is c1. If you execute git rebase master on the feature branch, the process is roughly as follows:

  • Find the latest commit node c5 of the current master branch, and change the base point of the feature branch to the c5 node. ;

  • If there is a conflict between the feature branch and the master branch, the conflict will be resolved sequentially based on the submission time of the feature branch, and the hash value of the commit of the feature branch will be modified.

  • Finally, when viewed on the branch, a straight line appears, but there is a problem of historical commit overwriting.

The result of the above process is shown below, where c2’ and c3’ indicate that the hash value has changed.

How to merge branch code with git

It is worth noting:

  • When performing the rebase operation, you need to ensure that the master branch is in the latest state, otherwise There may also be conflicts during the merge merge, which will lose the meaning of using rebase.

  • Never rebase content that has been pushed to the remote. If someone pulls the remote code, modifies it and submits it, the branch will become extremely troublesome.

After understanding the basic process, we can use the rebase command to start merging branches:

  • In the project Execute git rebase master as shown below. Because there are conflicts in both submissions, these conflicts need to be resolved sequentially in the rebase workspace.

How to merge branch code with git

Execute the gitk command on the feature branch and you can see it in the interface:
How to merge branch code with git

  • After the feature branch is rebased, switch back to the master branch and execute git merge feature to complete the merge operation.

How to merge branch code with git

Execute gitk on the master branch, the branch structure is as follows. You can see that the branches present a line, which looks very refreshing.

How to merge branch code with git

Instructions: git stash

Sometimes the code on the branch has not been developed yet and needs to be merged Branch, at this time you only need to:

1. Execute git stash to store the contents of the workspace, and then choose the above two methods of merging branches to merge branches.

How to merge branch code with git

2. After completing the branch merging, switch back to the development branch, execute git stash pop to pop up the contents of the workspace, and then you can continue writing code happily.

How to merge branch code with git

Summary

git merge is relatively crude and is also the method most people will choose. This method can ensure that each commit is Arranged in chronological order, but the branch diagram will be very messy and will introduce a meaningless commit.

git rebase is a line in the historical commit record, which is very elegant, but there is a risk of modifying the historical commit, and the commit timeline is messed up when viewing the log with git log. At the same time, remember that do not rebase content that has been pushed to the remote , otherwise the branch will become messy.

Personally, I tend to use the rebase method. After all, the cognitive cost of commit is there, and it looks comfortable. But if there are many developers, it’s better to merge. After all, resolving conflicts one by one will bore you to death, hahaha

in In projects, we always create many branches to develop different functions or requirements, and then merge them back into the main branch after the functions are completed. So how can you merge branches gracefully? If you are interested at this point, you might as well continue reading.

The above is the detailed content of How to merge branch code with 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