我如何在我的 Git 存储库中解决合并冲突?


当前回答

如果您不使用工具合并,首先复制您的代码:

- `checkout master`
- `git pull` / get new commit
- `git checkout` to your branch
- `git rebase master`

它解决冲突,你可以复制你的代码。

其他回答

我发现的最好的东西是使用“diff3”合并冲突风格:

<<<<<<<
Changes made on the branch that is being merged into. In most cases,
this is the branch that I have currently checked out (i.e. HEAD).
|||||||
The common ancestor version.
=======
Changes made on the branch that is being merged in. This is often a 
feature/topic branch.
>>>>>>>

如果冲突更长,那么我会把三个部分切成三个单独的文件,如“小”,“普通”和“天花板”。

diff common mine
diff common theirs

提示2

git log --merge -p <name of file>

如果你有自动测试,运行这些,如果你有绳子,运行它,如果这是一个可构建的项目,然后在你承诺之前建造它,等等,在所有情况下,你需要做一些测试,以确保你的变化没有打破任何东西。

例如,如果你知道你和另一个人都在不同的重复工作,这两者都会影响相同的文件集,你应该提前彼此谈话,并更好地了解你每个人都在做什么类型的变化。

如果你不确定合并,不要强迫它。

请遵循以下步骤来解决 Git 中的合并冲突:

查看 Git 状态: git 状态 获取 patchset: git fetch (从您的 Git commit 查看正确的 patch) Checkout 一个本地分支(temp1 在我的例子中在这里): git checkout -b temp1 Pull 最近的内容从主: git pull -rebase 起源主 启动 mergetool 并检查冲突并纠正它们......并检查与您当前分支的远程分支中的变化: git mergetool Chec

我一直遵循下面的步骤,以避免冲突。

git checkout master (到主分支) git pull (更新你的主分支以获得最新的代码) git checkout -b mybranch (检查一个新的分支,并开始在该分支工作,以便你的主分支总是留在顶部) git add. and git commit and git push (在你的本地分支后你的变化) git checkout master (回到你的主分支)

现在你可以做同样的事情,并保持你想要的多家当地分支机构,并同时工作,只要在需要时对你的分支机构进行吉特支票。

下面是可能的使用案例,从顶部:

你会拖一些变化,但哦,你没有到目前为止:

git fetch origin
git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.

所以你更新并尝试一次,但有冲突:

git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.

所以你决定看看这些变化:

git mergetool

哦,我的,哦,我的,上流改变了一些事情,但只是用我的变化......不......他们的变化......

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

然后,我们试着最后一次。

git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Already up-to-date.

是的!

有三个步骤:

查找哪些文件导致冲突的命令 git 状态 查看文件,在其中你会发现冲突标记为 <<<<<<<头 blablabla 将其改为你想要的方式,然后与命令 git 添加解决_conflicts_files git commit -m 混合 msg