强制 Git 冲突

Force a Git Conflict

为了我的工作,我做了一个关于 Git 的介绍演示。 我想展示如何解决合并冲突。但要这样做,我需要得到一个合并冲突。出于演示目的,我写了一个简单的 HTML 文档,在此文档中,参与者输入的是 Table 姓名。这足以产生冲突吗?

有趣的问题:)

使用 git checkout -b new-branch 创建一个新分支,编辑一行,提交。

切换到原始分支,用不同的编辑编辑同一行,再次提交。

现在 git merge new-branch,您将遇到合并冲突! :)

您可以产生多种冲突。通常人们会想到在不同的分支上以不同的方式修改一些行……这很好。那是一种冲突。但还有其他人。

  • 截取一段代码(文件中的一些行)并删除它们。取另一个分支并修改它们。然后合并。

  • 获取文件并将其删除。然后去另一个分支编辑它。然后合并。

  • 取一个文件并重命名。然后转到另一个分支并以其他方式重命名它。然后合并。

下面是如何产生冲突

# init your repo
git init

# print some text to any given file
echo 'aaa' > a.txt

# commit to the current branch
git add a.txt && git commit -m "Commit1"

# create a new branch
git checkout -b branch1

# add code to the end of the file
echo 'bbb' >> a.txt

# commit to the current branch (b)
git add a.txt && git commit -m "Commit2"

# get back to master branch
git checkout master

# add code to the end of the file
# here the file will still have its original code
echo 'ccc' >> a.txt

# commit to the current branch (master)
git add a.txt && git commit -m "Commit3"

# now when you will try to merge you will have conflict
git merge b