Home > Article > Development Tools > How to merge branch code with git
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.
// 初始化仓库 git init // 创建a.txt touch a.txt // 创建b.txt touch b.txt // 加入暂存区 git add . // 提交 git commit -m 'initial'
git checkout -b feature
The operation commands in the project are as follows. You can see that after executing the
git merge featurecommand, there are conflicts, enter the merging workspace, and then resolve all conflicts at once and submit a new commit.
Execute the gitk command line, and you can see the current branch on the interface as shown below. There is a new commit.
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.
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.
Execute the gitk command on the feature branch and you can see it in the interface:
After the feature branch is rebased, switch back to the master branch and execute git merge feature to complete the merge operation.
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.
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.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.
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!