merge conflicts

Merge conflicts (合并冲突)

Merge conflicts occur when multiple authors edit the same content or when one developer deletes a file while another developer was making changes on it. To solve this problem developers work in isolated branches. The git merge command is responsible for combining isolated branches and resolving conflicting edits.

Git makes the merging process easier unlike most version control systems. Git even can automatically integrate new changes. But in the cases of conflicts, Git cannot automatically figure out what version is correct. It marks the file as being conflicted and stops the merging process. (与大多数版本控制系统不同, Git使合并过程更容易。Git甚至可以自动集成新的更改。但在冲突的情况下, Git无法自动确定哪个版本是正确的。它将文件标记为冲突并停止合并过程。)

Types of merge conflicts

Types of merge conflicts (合并冲突的类型)

Conflicts can arise at two points: at the start of the merge process or during it.

Merge failure on start

Git fails to start a merge because the pending changes in the working directory or staging area of the project should be written over by the commits being merged in. This happens because of pending local changes. To take control over the local state, the git stash, git checkout, git commit and git reset commands are used. The following message will show up when an error occurs on start:

error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)

Failure during merge

The failure during merge tells that there is a conflict between the current local branch and the branch that is being merged in. The following message will show up when an error occurs during merge:

error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area)

Creating a merge conflict

Creating a merge conflict (创建合并冲突)

Here, we will show you a simulation of how merge conflicts appear. (在这里,我们将向您展示合并冲突如何出现的模拟。)

mkdir test-dir
cd test-dir
git init .
echo "some content" > example.txt
git add example.txt
git commit -am "initial commit"
[master (root-commit) a45c22d] initial commit
1 file changed, 1 insertion(+)
create mode 100524 example.txt

In the given example, we create a test-dir new directory. Next, we create example.txt text file with some content and add it to the repository and commit it. As a result, we have a new repository with one master branch and example.txt file. The next step is creating another branch to use as a conflicting merge. (在给定的示例中,我们创建了一个test-dir新目录。接下来,我们使用一些内容创建example.txt文本文件,并将其添加到存储库并提交。因此,我们有了一个新的存储库,其中有一个master分支和example.txt文件。下一步是创建另一个分支以用作冲突合并。)

git checkout -b branch_to_merge
echo "completely different content to merge later" > example.txt
git commit -am "edit the content of example.txt to make a conflict"
[branch_to_merge 4221135] edit the content of example.txt to make a conflict
1 file changed, 1 insertion(+), 1 deletion(-)

In the above example, we create and check out branch_to_merge branch. After creating, we overwrite the content in example.txt file and commit the new content. After doing all this, the commit overrides the content of example.txt:

git checkout master
Switched to branch 'master'
echo "content to add" >> example.txt
git commit -am "added content to example.txt"
[master 11ab34b] added content to example.txt
1 file changed, 1 insertion(+)

This bunch of commands checks out the master branch attaching the content to example.txt and committing it. So, our repository is put to the state where we have one commit in the master branch and one in the branch_to_merge branch. The final step is to execute the git merge command after which conflict will occur:

git merge branch_to_merge
Auto-merging example.txt
CONFLICT (content): Merge conflict in example.txt
Automatic merge failed; fix conflicts and then commit the result.

Identifying merge conflicts

Identifying merge conflicts (识别合并冲突)

As we have already seen, Git displays output which indicates that a conflict has appeared. Execute the git status command to see the unmerged paths:

git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified:   example.txt

The example.txt file appears in a modified state. Execute cat command to put out the contents of the example.txt file. We can see these visual marks:

<<<<<<< HEAD
=======
>>>>>>> branch_to_merge

The ======= marks is the center of the conflict. The content between the center and the HEAD line is the content existing in the current branch master that the HEAD reference is pointing to. Read more about visual marks on the git merge page.

Resolving merge conflicts

Resolving merge conflicts (解决合并冲突)

To resolve a merge conflict you should edit the conflicted file. Open the example.txt file in the editor and remove all the marks. The changed file has the following look:

some content to mess with
content to add
completely different content to merge later

Execute the git add the command to stage the new merge content. Next, create a new commit to complete the merge:

git commit -m "the conflict in example.txt is merged and resolved"
General Tools
git status
git log –merge
git diff
git checkout
git reset –mixed
git merge –abort
git reset


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部