Is there a good way to explain how to resolve merge conflicts in Git?
有没有一种很好的方法来解释如何解决Git中的合并冲突?
2505
Try: git mergetool
试试:git mergetool
It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. It is much better than doing the whole thing by hand certainly.
它会打开一个GUI,引导您完成每个冲突,然后您可以选择合并方式。有时它需要稍后进行一些手工编辑,但通常它本身就足够了。这肯定比手工做整件事要好得多。
As per @JoshGlover comment:
根据@JoshGlover评论:
The command doesn't necessarily open a GUI unless you install one. Running git mergetool
for me resulted in vimdiff
being used. You can install one of the following tools to use it instead: meld
, opendiff
, kdiff3
, tkdiff
, xxdiff
, tortoisemerge
, gvimdiff
, diffuse
, ecmerge
, p4merge
, araxis
, vimdiff
, emerge
.
除非您安装GUI,否则该命令不一定会打开GUI。为我运行git mergetool导致使用vimdiff。您可以安装以下工具之一来代替使用它:meld,opendiff,kdiff3,tkdiff,xxdiff,tortoisemerge,gvimdiff,diffuse,ecmerge,p4merge,araxis,vimdiff,emerge。
Below is the sample procedure to use vimdiff
for resolve merge conflicts. Based on this link
下面是使用vimdiff解决合并冲突的示例过程。基于此链接
Step 1: Run following commands in your terminal
步骤1:在终端中运行以下命令
git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false
This will set vimdiff as the default merge tool.
这会将vimdiff设置为默认合并工具。
Step 2: Run following command in terminal
步骤2:在终端中运行以下命令
git mergetool
Step 3: You will see a vimdiff display in following format
第3步:您将看到以下格式的vimdiff显示
+----------------------+
| | | |
|LOCAL |BASE |REMOTE |
| | | |
+----------------------+
| MERGED |
| |
+----------------------+
These 4 views are
这4个观点是
LOCAL – this is file from the current branch
LOCAL - 这是当前分支的文件
BASE – common ancestor, how file looked before both changes
BASE - 共同的祖先,文件在两个变化之前看起来如何
REMOTE – file you are merging into your branch
REMOTE - 您正在合并到您的分支机构的文件
MERGED – merge result, this is what gets saved in the repo
合并 - 合并结果,这是在回购中保存的内容
You can navigate among these views using ctrl+w
. You can directly reach MERGED view using ctrl+w
followed by j
.
您可以使用ctrl + w在这些视图中导航。您可以使用ctrl + w后跟j直接到达MERGED视图。
More info about vimdiff navigation here and here
有关vimdiff navigation更多信息,请点击此处
Step 4. You could edit the MERGED view the following way
步骤4.您可以通过以下方式编辑MERGED视图
If you want to get changes from REMOTE
如果您想从REMOTE获得更改
:diffg RE
If you want to get changes from BASE
如果您想从BASE获得更改
:diffg BA
If you want to get changes from LOCAL
如果您想从LOCAL获得更改
:diffg LO
Step 5. Save, Exit, Commit and Clean up
步骤5.保存,退出,提交和清理
:wqa
save and exit from vi
:wqa保存并退出vi
git commit -m "message"
git commit -m“message”
git clean
Remove extra files (e.g. *.orig) created by diff tool.
git clean删除diff工具创建的额外文件(例如* .orig)。
1622
Here's a probable use-case, from the top:
这是一个可能的用例,从顶部:
You're going to pull some changes, but oops, you're not up to date:
你要做一些改变,但是哎呀,你不是最新的:
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.
So you get up-to-date and try again, but have a conflict:
所以你得到最新的并再试一次,但是有冲突:
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.
So you decide to take a look at the changes:
所以你决定看一下这些变化:
git mergetool
Oh me, oh my, upstream changed some things, but just to use my changes...no...their changes...
哦,我,我的,上游改变了一些事情,但只是为了使用我的改变......不......他们的改变......
git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"
And then we try a final time
然后我们尝试最后一次
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Already up-to-date.
Ta-da!
694
I find merge tools rarely help me understand the conflict or the resolution. I'm usually more successful looking at the conflict markers in a text editor and using git log as a supplement.
我发现合并工具很少帮助我理解冲突或解决方案。我通常更成功地在文本编辑器中查看冲突标记并使用git log作为补充。
Here are a few tips:
以下是一些提示:
The best thing I have found is to use the "diff3" merge conflict style:
我发现的最好的事情是使用“diff3”合并冲突风格:
git config merge.conflictstyle diff3
git config merge.conflictstyle diff3
This produces conflict markers like this:
这会产生如下冲突标记:
<<<<<<<
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.
>>>>>>>
The middle section is what the common ancestor looked like. This is useful because you can compare it to the top and bottom versions to get a better sense of what was changed on each branch, which gives you a better idea for what the purpose of each change was.
中间部分是共同祖先的样子。这很有用,因为您可以将它与顶部和底部版本进行比较,以更好地了解每个分支上的更改内容,从而更好地了解每个更改的目的。
If the conflict is only a few lines, this generally makes the conflict very obvious. (Knowing how to fix a conflict is very different; you need to be aware of what other people are working on. If you're confused, it's probably best to just call that person into your room so they can see what you're looking at.)
如果冲突只有几行,这通常会使冲突非常明显。 (知道如何解决冲突是非常不同的;你需要知道其他人正在做什么。如果你感到困惑,最好是把那个人叫到你的房间,这样他们就能看到你在看什么在。)
If the conflict is longer, then I will cut and paste each of the three sections into three separate files, such as "mine", "common" and "theirs".
如果冲突时间较长,那么我会将三个部分中的每一部分剪切并粘贴到三个单独的文件中,例如“我的”,“普通”和“他们的”。
Then I can run the following commands to see the two diff hunks that caused the conflict:
然后我可以运行以下命令来查看导致冲突的两个差异:
diff common mine
diff common theirs
This is not the same as using a merge tool, since a merge tool will include all of the non-conflicting diff hunks too. I find that to be distracting.
这与使用合并工具不同,因为合并工具也将包括所有非冲突的差异。我觉得这会分散注意力。
Somebody already mentioned this, but understanding the intention behind each diff hunk is generally very helpful for understanding where a conflict came from and how to handle it.
有人已经提到了这一点,但了解每个差异背后的意图通常对于了解冲突来自何处以及如何处理它非常有帮助。
git log --merge -p <name of file>
This shows all of the commits that touched that file in between the common ancestor and the two heads you are merging. (So it doesn't include commits that already exist in both branches before merging.) This helps you ignore diff hunks that clearly are not a factor in your current conflict.
这显示了在共同祖先和要合并的两个头之间触及该文件的所有提交。 (因此它不包括在合并之前已存在于两个分支中的提交。)这有助于您忽略明显不是当前冲突因素的差异。
Verify your changes with automated tools.
使用自动化工具验证您的更改。
If you have automated tests, run those. If you have a lint, run that. If it's a buildable project, then build it before you commit, etc. In all cases, you need to do a bit of testing to make sure your changes didn't break anything. (Heck, even a merge without conflicts can break working code.)
如果您有自动化测试,请运行它们。如果你有一个棉绒,那就跑吧。如果它是一个可构建的项目,那么在你提交之前构建它等等。在所有情况下,你需要做一些测试,以确保你的更改没有破坏任何东西。 (哎呀,即使没有冲突的合并也会破坏工作代码。)
Plan ahead; communicate with co-workers.
未雨绸缪;与同事沟通。
Planning ahead and being aware of what others are working on can help prevent merge conflicts and/or help resolve them earlier -- while the details are still fresh in mind.
提前规划并了解其他人正在进行的工作可以帮助防止合并冲突和/或帮助他们提前解决 - 而细节仍然是新鲜的。
For example, if you know that you and another person are both working on different refactoring that will both affect the same set of files, you should talk to each other ahead of time and get a better sense for what types of changes each of you is making. You might save considerable time and effort if you conduct your planned changes serially rather than in parallel.
例如,如果您知道您和另一个人都在进行不同的重构,这两个重构都会影响同一组文件,那么您应该提前相互交谈并更好地了解每个人的变化类型。制造。如果您按顺序而不是并行地执行计划的更改,则可能会节省大量时间和精力。
For major refactorings that cut across a large swath of code, you should strongly consider working serially: everybody stops working on that area of the code while one person performs the complete refactoring.
对于跨越大量代码的主要重构,您应该强烈考虑连续工作:当一个人执行完整的重构时,每个人都停止在代码的该区域工作。
If you can't work serially (due to time pressure, maybe), then communicating about expected merge conflicts at least helps you solve the problems sooner while the details are still fresh in mind. For example, if a co-worker is making a disruptive series of commits over the course of a one-week period, you may choose to merge/rebase on that co-workers branch once or twice each day during that week. That way, if you do find merge/rebase conflicts, you can solve them more quickly than if you wait a few weeks to merge everything together in one big lump.
如果你不能连续工作(由于时间压力,也许),那么沟通预期的合并冲突至少可以帮助你更快地解决问题,同时细节仍然是新鲜的。例如,如果同事在一周的时间内进行了一系列破坏性的提交,您可以选择在该周期间每天一次或两次合并/重组该同事分支。这样,如果你确实发现了合并/ rebase冲突,你可以比等待几周将所有内容合并成一个大块的更快地解决它们。
If you're unsure of a merge, don't force it.
如果您不确定合并,请不要强迫它。
Merging can feel overwhelming, especially when there are a lot of conflicting files and the conflict markers cover hundreds of lines. Often times when estimating software projects we don't include enough time for overhead items like handling a gnarly merge, so it feels like a real drag to spend several hours dissecting each conflict.
合并可能会让人感到压力,特别是当存在大量冲突文件且冲突标记覆盖数百行时。通常在评估软件项目时,我们没有足够的时间来处理开销项目,例如处理粗糙的合并,因此花费几个小时来解决每个冲突感觉真的很麻烦。
In the long run, planning ahead and being aware of what others are working on are the best tools for anticipating merge conflicts and prepare yourself to resolve them correctly in less time.
从长远来看,提前规划并了解其他人正在开展的工作是预测合并冲突并准备好在更短的时间内正确解决冲突的最佳工具。
325
Identify which files are in conflict (Git should tell you this).
确定哪些文件存在冲突(Git应该告诉您)。
Open each file and examine the diffs; Git demarcates them. Hopefully it will be obvious which version of each block to keep. You may need to discuss it with fellow developers who committed the code.
打开每个文件并检查差异; Git划定了他们。希望很明显每个块的哪个版本要保留。您可能需要与提交代码的开发人员讨论它。
Once you've resolved the conflict in a file git add the_file
.
一旦你解决了文件git中的冲突,就添加the_file。
Once you've resolved all conflicts, do git rebase --continue
or whatever command Git said to do when you completed.
一旦解决了所有冲突,请执行git rebase - 继续或Git在完成时要执行的任何命令。
100
Check out the answers in Stack Overflow question Aborting a merge in Git, especially Charles Bailey's answer which shows how to view the different versions of the file with problems, for example,
查看Stack Overflow问题中的答案在Git中中止合并,特别是Charles Bailey的答案,其中显示了如何查看有问题的文件的不同版本,例如,
# Common base version of the file.
git show :1:some_file.cpp
# 'Ours' version of the file.
git show :2:some_file.cpp
# 'Theirs' version of the file.
git show :3:some_file.cpp
90
Merge conflicts happens when changes are made to a file at the same time. Here is how to solve it.
当同时对文件进行更改时,会发生合并冲突。这是如何解决它。
git
CLIHere are simple steps what to do when you get into conflicted state:
以下是进入冲突状态时要执行的操作的简单步骤:
git status
(under Unmerged paths
section).请注意冲突文件列表:git status(在Unmerged paths部分下)。
Solve the conflicts separately for each file by one of the following approaches:
通过以下方法之一单独解决每个文件的冲突:
Use GUI to solve the conflicts: git mergetool
(the easiest way).
使用GUI解决冲突:git mergetool(最简单的方法)。
To accept remote/other version, use: git checkout --theirs path/file
. This will reject any local changes you did for that file.
要接受远程/其他版本,请使用:git checkout --thehis path / file。这将拒绝您对该文件所做的任何本地更改。
To accept local/our version, use: git checkout --ours path/file
要接受本地/我们的版本,请使用:git checkout --ours path / file
However you've to be careful, as remote changes that conflicts were done for some reason.
但是你要小心,因为远程改变冲突是出于某种原因。
Related: What is the precise meaning of "ours" and "theirs" in git?
相关:git中“我们的”和“他们的”的确切含义是什么?
Edit the conflicted files manually and look for the code block between <<<<<
/>>>>>
then choose the version either from above or below =====
. See: How conflicts are presented.
手动编辑冲突的文件并查找<<<<< / >>>>>之间的代码块,然后从上方或下方选择版本=====。请参阅:如何呈现冲突。
Path and filename conflicts can be solved by git add
/git rm
.
路径和文件名冲突可以通过git add / git rm解决。
Finally, review the files ready for commit using: git status
.
最后,使用:git status查看准备提交的文件。
If you still have any files under Unmerged paths
, and you did solve the conflict manually, then let Git know that you solved it by: git add path/file
.
如果您仍然在Unmerged路径下有任何文件,并且您确实手动解决了冲突,那么让Git知道您通过以下方式解决了它:git add path / file。
If all conflicts were solved successfully, commit the changes by: git commit -a
and push to remote as usual.
如果所有冲突都已成功解决,请通过以下方式提交更改:git commit -a并像往常一样推送到远程。
See also: Resolving a merge conflict from the command line at GitHub
另请参阅:从GitHub的命令行解决合并冲突
I've successfully used DiffMerge which can visually compare and merge files on Windows, macOS and Linux/Unix.
我已成功使用DiffMerge,它可以在Windows,macOS和Linux / Unix上直观地比较和合并文件。
It graphically can show the changes between 3 files and it allows automatic merging (when safe to do so) and full control over editing the resulting file.
它以图形方式显示3个文件之间的变化,它允许自动合并(在安全的情况下),并完全控制编辑生成的文件。
Image source: DiffMerge (Linux screenshot)
图像源:DiffMerge(Linux截图)
Simply download it and run in repo as:
只需下载并在repo中运行:
git mergetool -t diffmerge .
On macOS you can install via:
在macOS上,您可以通过以下方式安装
brew install caskroom/cask/brew-cask
brew cask install diffmerge
And probably (if not provided) you need the following extra simple wrapper placed in your PATH (e.g. /usr/bin
):
并且可能(如果没有提供)你需要在PATH中放置以下额外的简单包装器(例如/ usr / bin):
#!/bin/sh
DIFFMERGE_PATH=/Applications/DiffMerge.app
DIFFMERGE_EXE=${DIFFMERGE_PATH}/Contents/MacOS/DiffMerge
exec ${DIFFMERGE_EXE} --nosplash "$@"
Then you can use the following keyboard shortcuts:
然后您可以使用以下键盘快捷键:
⌘-Alt-Up / Down可跳转到上一个/下一个更改。
⌘-Alt-Left / Right接受左侧或右侧的更改
Alternatively you can use opendiff (part of Xcode Tools) which lets you merge two files or directories together to create a third file or directory.
或者,您可以使用opendiff(Xcode Tools的一部分),它允许您将两个文件或目录合并在一起以创建第三个文件或目录。
74
If you're making frequent small commits, then start by looking at the commit comments with git log --merge
. Then git diff
will show you the conflicts.
如果您经常进行小型提交,那么首先使用git log --merge查看提交注释。然后git diff会告诉你冲突。
For conflicts that involve more than a few lines, it's easier to see what's going on in an external GUI tool. I like opendiff -- Git also supports vimdiff, gvimdiff, kdiff3, tkdiff, meld, xxdiff, emerge out of the box and you can install others: git config merge.tool "your.tool"
will set your chosen tool and then git mergetool
after a failed merge will show you the diffs in context.
对于涉及多行的冲突,更容易看到外部GUI工具中发生了什么。我喜欢opendiff - Git也支持vimdiff,gvimdiff,kdiff3,tkdiff,meld,xxdiff,emerge开箱即用,你可以安装别人:git config merge.tool“your.tool”将设置你选择的工具然后git mergetool合并失败后会在上下文中显示差异。
Each time you edit a file to resolve a conflict, git add filename
will update the index and your diff will no longer show it. When all the conflicts are handled and their files have been git add
-ed, git commit
will complete your merge.
每次编辑文件以解决冲突时,git add filename都会更新索引,您的差异将不再显示。当处理完所有冲突并且他们的文件已经被git add-ed时,git commit将完成你的合并。
43
See How Conflicts Are Presented or, in Git, the git merge
documentation to understand what merge conflict markers are.
请参阅如何提供冲突,或者在Git中,使用git merge文档来了解合并冲突标记是什么。
Also, the How to Resolve Conflicts section explains how to resolve the conflicts:
此外,“如何解决冲突”部分介绍了如何解决冲突:
After seeing a conflict, you can do two things:
看到冲突后,你可以做两件事:
Decide not to merge. The only clean-ups you need are to reset the index file to the
HEAD
commit to reverse 2. and to clean up working tree changes made by 2. and 3.;git merge --abort
can be used for this.决定不合并。您需要的唯一清理是将索引文件重置为HEAD提交以反转2.并清除由2.和3进行的工作树更改。 git merge --abort可以用于此。
Resolve the conflicts. Git will mark the conflicts in the working tree. Edit the files into shape and
git add
them to the index. Usegit commit
to seal the deal.解决冲突。 Git将标记工作树中的冲突。将文件编辑为形状,然后将它们添加到索引中。使用git commit来达成交易。
You can work through the conflict with a number of tools:
您可以使用许多工具解决冲突:
Use a mergetool.
git mergetool
to launch a graphical mergetool which will work you through the merge.使用mergetool。 git mergetool启动图形合并工具,它将帮助您完成合并。
Look at the diffs.
git diff
will show a three-way diff, highlighting changes from both theHEAD
andMERGE_HEAD
versions.看看差异。 git diff将显示一个三向差异,突出显示HEAD和MERGE_HEAD版本的变化。
Look at the diffs from each branch.
git log --merge -p <path>
will show diffs first for theHEAD
version and then theMERGE_HEAD
version.查看每个分支的差异。 git log --merge -p
将首先显示HEAD版本的差异,然后显示MERGE_HEAD版本。 Look at the originals.
git show :1:filename
shows the common ancestor,git show :2:filename
shows theHEAD
version, andgit show :3:filename
shows theMERGE_HEAD
version.看看原件。 git show:1:filename显示共同的祖先,git show:2:filename显示HEAD版本,git show:3:filename显示MERGE_HEAD版本。
You can also read about merge conflict markers and how to resolve them in the Pro Git book section Basic Merge Conflicts.
您还可以在Pro Git一书的基本合并冲突部分中阅读有关合并冲突标记以及如何解决它们的信息。
36
For Emacs users which want to resolve merge conflicts semi-manually:
对于想要半手动解决合并冲突的Emacs用户:
git diff --name-status --diff-filter=U
shows all files which require conflict resolution.
显示需要解决冲突的所有文件。
Open each of those files one by one, or all at once by:
逐个打开每个文件,或者一次全部打开:
emacs $(git diff --name-only --diff-filter=U)
When visiting a buffer requiring edits in Emacs, type
访问需要在Emacs中编辑的缓冲区时,请键入
ALT+x vc-resolve-conflicts
This will open three buffers (mine, theirs, and the output buffer). Navigate by pressing 'n' (next region), 'p' (prevision region). Press 'a' and 'b' to copy mine or theirs region to the output buffer, respectively. And/or edit the output buffer directly.
这将打开三个缓冲区(我的,他们的和输出缓冲区)。按'n'(下一个区域),'p'(预视区域)进行导航。按'a'和'b'分别将我或他们的区域复制到输出缓冲区。和/或直接编辑输出缓冲区。
When finished: Press 'q'. Emacs asks you if you want to save this buffer: yes. After finishing a buffer mark it as resolved by running from the teriminal:
完成后:按'q'。 Emacs会询问您是否要保存此缓冲区:是的。完成一个缓冲区后,通过从teriminal运行解决它:
git add FILENAME
When finished with all buffers type
完成所有缓冲区类型后
git commit
to finish the merge.
完成合并。
28
Please follow the following steps to fix merge conflicts in Git:
请按照以下步骤修复Git中的合并冲突:
Check the Git status: git status
检查Git状态:git status
Get the patchset: git fetch (checkout the right patch from your Git commit)
获取补丁集:git fetch(从Git提交中检出正确的补丁)
Checkout a local branch (temp1 in my example here): git checkout -b temp1
签出一个本地分支(我的例子中的temp1):git checkout -b temp1
Pull the recent contents from master: git pull --rebase origin master
从master中拉出最近的内容:git pull --rebase origin master
Start the mergetool and check the conflicts and fix them...and check the changes in the remote branch with your current branch: git mergetool
启动mergetool并检查冲突并修复它们...并使用当前分支检查远程分支中的更改:git mergetool
Check the status again: git status
再次检查状态:git status
Delete the unwanted files locally created by mergetool, usually mergetool creates extra file with *.orig extension. Please delete that file as that is just the duplicate and fix changes locally and add the correct version of your files. git add #your_changed_correct_files
删除mergetool本地创建的不需要的文件,通常mergetool创建带* .orig扩展名的额外文件。请删除该文件,因为它只是重复文件并在本地修复更改并添加正确版本的文件。 git add #your_changed_correct_files
Check the status again: git status
再次检查状态:git status
Commit the changes to the same commit id (this avoids a new separate patch set): git commit --amend
将更改提交给相同的提交ID(这可以避免新的单独补丁集):git commit --amend
Push to the master branch: git push (to your Git repository)
推送到主分支:git push(到你的Git仓库)
27
Simply, if you know well that changes in one of the repositories is not important, and want to resolve all changes in favor of the other one, use:
简单地说,如果您清楚其中一个存储库中的更改并不重要,并希望解决所有更改以支持另一个存储库,请使用:
git checkout . --ours
to resolve changes in the favor of your repository, or
解决有关您的存储库的更改,或
git checkout . --theirs
to resolve changes in favor of the other or the main repository.
解决有利于其他或主存储库的更改。
Or else you will have to use a GUI merge tool to step through files one by one, say the merge tool is p4merge
, or write any one's name you've already installed
否则你将不得不使用GUI合并工具逐个浏览文件,比如合并工具是p4merge,或者写你已经安装的任何一个名字
git mergetool -t p4merge
and after finishing a file, you will have to save and close, so the next one will open.
完成文件后,您必须保存并关闭,因此下一个文件将打开。
27
I either want my or their version in full, or want to review individual changes and decide for each of them.
我要么完全想要我或他们的版本,要么想要审查个别更改并决定每个更改。
Fully accept my or theirs version:
完全接受我或他们的版本:
Accept my version (local, ours):
接受我的版本(本地,我们的):
git checkout --ours -- <filename>
git add <filename> # Marks conflict as resolved
git commit -m "merged bla bla" # An "empty" commit
Accept their version (remote, theirs):
接受他们的版本(远程,他们的):
git checkout --theirs -- <filename>
git add <filename>
git commit -m "merged bla bla"
If you want to do for all conflict files run:
如果要对所有冲突文件执行运行:
git merge --strategy-option ours
or
git merge --strategy-option theirs
Review all changes and accept them individually
检查所有更改并单独接受它们
git mergetool
查看更改并接受每个版本的任一版本。
git add <filename>
git add
git commit -m "merged bla bla"
git commit -m“merged bla bla”
Default mergetool
works in command line. How to use a command line mergetool should be a separate question.
默认的mergetool在命令行中工作。如何使用命令行mergetool应该是一个单独的问题。
You can also install visual tool for this, e.g. meld
and run
您也可以为此安装可视化工具,例如融合并奔跑
git mergetool -t meld
It will open local version (ours), "base" or "merged" version (the current result of the merge) and remote version (theirs). Save the merged version when you are finished, run git mergetool -t meld
again until you get "No files need merging", then go to Steps 3. and 4.
它将打开本地版本(我们的),“基础”或“合并”版本(合并的当前结果)和远程版本(他们的)。完成后保存合并的版本,再次运行git mergetool -t meld,直到得到“No files need merge”,然后转到步骤3和4。
25
You could fix merge conflicts in a number of ways as other have detailed.
您可以通过多种方式修复合并冲突,其他方面已详细说明。
I think the real key is knowing how changes flow with local and remote repositories. The key to this is understanding tracking branches. I have found that I think of the tracking branch as the 'missing piece in the middle' between me my local, actual files directory and the remote defined as origin.
我认为真正的关键是了解变化如何与本地和远程存储库一起流动。关键是要了解跟踪分支。我发现我认为跟踪分支是我本地的实际文件目录和定义为原点的远程之间的“中间缺失的部分”。
I've personally got into the habit of 2 things to help avoid this.
我个人习惯了两件事来帮助避免这种情况。
Instead of:
git add .
git commit -m"some msg"
Which has two drawbacks -
这有两个缺点 -
a) All new/changed files get added and that might include some unwanted changes.
b) You don't get to review the file list first.
a)添加所有新的/更改的文件,其中可能包含一些不需要的更改。 b)您无法先查看文件列表。
So instead I do:
所以相反,我做:
git add file,file2,file3...
git commit # Then type the files in the editor and save-quit.
This way you are more deliberate about which files get added and you also get to review the list and think a bit more while using the editor for the message. I find it also improves my commit messages when I use a full screen editor rather than the -m
option.
通过这种方式,您可以更加深思熟虑地添加哪些文件,并且还可以在使用编辑器查看消息时查看列表并进行更多思考。当我使用全屏编辑器而不是-m选项时,我发现它也改进了我的提交消息。
[Update - as time has passed I've switched more to:
[更新 - 随着时间的推移,我已经切换到:
git status # Make sure I know whats going on
git add .
git commit # Then use the editor
]
Also (and more relevant to your situation), I try to avoid:
另外(和你的情况更相关),我尽量避免:
git pull
or
git pull origin master.
because pull implies a merge and if you have changes locally that you didn't want merged you can easily end up with merged code and/or merge conflicts for code that shouldn't have been merged.
因为pull意味着合并,如果您在本地进行了不希望合并的更改,则很容易就会出现合并代码和/或合并冲突,因为这些代码不应该合并。
Instead I try to do
相反,我尝试做
git checkout master
git fetch
git rebase --hard origin/master # or whatever branch I want.
You may also find this helpful:
您可能还会发现这有用:
git branch, fork, fetch, merge, rebase and clone, what are the differences?
git branch,fork,fetch,merge,rebase和clone,有什么区别?
25
In speaking of pull/fetch/merge in the above answers, I would like to share an interesting and productive trick,
在上述答案中谈到拉/取/合并时,我想分享一个有趣且富有成效的技巧,
git pull --rebase
This above command is the most useful command in my git life which saved a lots of time.
上面的命令是我git生活中最有用的命令,节省了大量的时间。
Before pushing your newly committed change to remote server, try git pull --rebase
rather git pull
and manual merge
and it will automatically sync latest remote server changes (with a fetch + merge) and will put your local latest commit at the top in git log. No need to worry about manual pull/merge.
在将新提交的更改推送到远程服务器之前,请尝试git pull --rebase而不是git pull和手动合并,它将自动同步最新的远程服务器更改(使用fetch + merge)并将您的本地最新提交放在git的顶部登录。无需担心手动拉/合并。
In case of conflict, just use
如果发生冲突,请使用
git mergetool
git add conflict_file
git rebase --continue
Find details at: http://gitolite.com/git-pull--rebase
有关详细信息,请访问:http://gitolite.com/git-pull--rebase
22
CoolAJ86's answer sums up pretty much everything. In case you have changes in both branches in the same piece of code you will have to do a manual merge. Open the file in conflict in any text editor and you should see following structure.
CoolAJ86的答案总结了几乎所有东西。如果您在同一段代码中的两个分支都有更改,则必须进行手动合并。在任何文本编辑器中打开冲突文件,您应该看到以下结构。
(Code not in Conflict)
>>>>>>>>>>>
(first alternative for conflict starts here)
Multiple code lines here
===========
(second alternative for conflict starts here)
Multiple code lines here too
<<<<<<<<<<<
(Code not in conflict here)
Choose one of the alternatives or a combination of both in a way that you want new code to be, while removing equal signs and angle brackets.
以您希望新代码的方式选择其中一个替代方案或两者的组合,同时删除等号和尖括号。
git commit -a -m "commit message"
git push origin master
16
There are 3 steps:
有3个步骤:
Find which files cause conflicts by command
通过命令查找哪些文件导致冲突
git status
Check the files, in which you would find the conflicts marked like
检查文件,在其中您会发现标记为的冲突
<<<<<<<<head
blablabla
Change it to the way you want it, then commit with commands
将其更改为您想要的方式,然后使用命令进行提交
git add solved_conflicts_files
git commit -m 'merge msg'
15
git log --merge -p [[--] path]
Does not seem to always work for me and usually ends up displaying every commit that was different between the two branches, this happens even when using --
to separate the path from the command.
似乎并不总是对我有用,并且通常最终会显示两个分支之间不同的每个提交,即使使用 - 将路径与命令分开也会发生这种情况。
What I do to work around this issue is open up two command lines and in one run
我解决这个问题的方法是在一次运行中打开两个命令行
git log ..$MERGED_IN_BRANCH --pretty=full -p [path]
and in the other
而在另一方面
git log $MERGED_IN_BRANCH.. --pretty=full -p [path]
Replacing $MERGED_IN_BRANCH
with the branch I merged in and [path]
with the file that is conflicting. This command will log all the commits, in patch form, between (..
) two commits. If you leave one side empty like in the commands above git will automatically use HEAD
(the branch you are merging into in this case).
将$ MERGED_IN_BRANCH替换为我合并的分支,将[path]替换为冲突的文件。此命令将以补丁形式记录(..)两次提交之间的所有提交。如果你把一方留空,就像上面的命令一样,git将自动使用HEAD(在这种情况下你要合并的分支)。
This will allow you to see what commits went into the file in the two branches after they diverged. It usually makes it much easier to solve conflicts.
这将允许您查看两个分支在分叉后进入文件的提交。它通常可以更容易地解决冲突。
15
As of December 12th 2016, you can merge branches and resolve conflicts on github.com
截至2016年12月12日,您可以在github.com上合并分支并解决冲突
Thus, if you don't want to use the command-line or any 3rd party tools that are offered here from older answers, go with GitHub's native tool.
因此,如果您不想使用命令行或此处提供的任何第三方工具,请使用GitHub的本机工具。
This blog post explains in detail, but the basics are that upon 'merging' two branches via the UI, you will now see a 'resolve conflicts' option that will take you to an editor allowing you to deal with these merge conflicts.
这篇博文详细解释了,但基本原理是,在通过UI“合并”两个分支后,您现在将看到一个“解决冲突”选项,它将带您到编辑器,允许您处理这些合并冲突。
14
patience
I'm surprised no one else spoke about resolving conflict using patience
with the merge recursive strategy. For a big merge conflict, using patience
provided good results for me. The idea is that it will try to match blocks rather than individual lines.
我很惊讶没有人谈到使用合并递归策略耐心解决冲突。对于大合并冲突,使用耐心为我提供了良好的结果。我们的想法是尝试匹配块而不是单独的线。
If you change the indentation of your program for instance, the default Git merge strategy sometimes matches single braces {
which belongs to different functions. This is avoided with patience
:
例如,如果更改程序的缩进,默认的Git合并策略有时会匹配单个大括号{属于不同的函数。耐心地避免这种情况:
git merge -s recursive -X patience other-branch
From the documentation:
从文档:
With this option, merge-recursive spends a little extra time to avoid
mismerges that sometimes occur due to unimportant matching lines
(e.g., braces from distinct functions). Use this when the branches to
be merged have diverged wildly.
If you have a merge conflict and want to see what others had in mind when modifying their branch, it's sometimes easier to compare their branch directly with the common ancestor (instead of our branch). For that you can use merge-base
:
如果你有一个合并冲突并且想要在修改他们的分支时看到其他人的想法,有时更容易将他们的分支直接与共同的祖先(而不是我们的分支)进行比较。为此你可以使用merge-base:
git diff $(git merge-base <our-branch> <their-branch>) <their-branch>
Usually, you only want to see the changes for a particular file:
通常,您只想查看特定文件的更改:
git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>
12
I always follow the below steps to avoid conflicts.
我总是按照以下步骤来避免冲突。
git checkout master(来到master分支)
git pull(更新你的主人以获取最新的代码)
git checkout -b mybranch(签出一个新的分支并开始在该分支上工作,这样你的主人总是保持在主干之上。)
git add。和git commit AND git push(在你的更改后在你的本地分支上)
git checkout master(回到你的主人那里。)
Now you can do the same and maintain as many local branches you want and work simultaneous my just doing a git checkout to your branch when ever necessary.
现在你可以做同样的事情并维护你想要的许多本地分支机构并同时工作我只需要在你的分支机构进行git checkout。
11
If you want to merge from branch (test) to master, you can follow these steps:
如果要从分支(测试)合并到主服务器,可以按照以下步骤操作:
Step 1: Go to the branch
第1步:转到分支机构
git checkout test
Step 2: git pull --rebase origin master
第2步:git pull --rebase origin master
Step 3: If there are some conflicts, go to these files to modify it.
步骤3:如果存在一些冲突,请转到这些文件进行修改。
Step 4: Add these changes
第4步:添加这些更改
git add #your_changes_files
Step 5: git rebase --continue
第5步:git rebase - 继续
Step 6: if there is still conflict, go back to step 3 again. If there is no conflict, do following: git push origin +test
步骤6:如果仍有冲突,请再次返回步骤3。如果没有冲突,请执行以下操作:git push origin + test
Step 7: And then there is no conflict between test and master. You can use merge directly.
第7步:然后测试和主人之间没有冲突。您可以直接使用合并。
10
Merge conflicts could occur in different situations:
合并冲突可能发生在不同的情况:
运行“git fetch”然后“git merge”
运行“git fetch”然后“git rebase”
运行“git pull”(实际上等于上述条件之一)
当运行“git stash pop”时
当您应用git补丁(导出到要传输的文件的提交,例如,通过电子邮件)
You need to install a merge tool which is compatible with Git to resolve the conflicts. I personally use KDiff3, and I've found it nice and handy. You can download its Windows version here:
您需要安装与Git兼容的合并工具来解决冲突。我个人使用KDiff3,我发现它很好用。您可以在此处下载其Windows版本:
https://sourceforge.net/projects/kdiff3/files/
BTW if you install Git Extensions there is an option in its setup wizard to install Kdiff3.
顺便说一句,如果你安装Git Extensions,它的安装向导中有一个选项来安装Kdiff3。
Then setup git configs to use Kdiff as its mergetool:
然后设置git configs使用Kdiff作为其mergetool:
$ git config --global --add merge.tool kdiff3
$ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add mergetool.kdiff3.trustExitCode false
$ git config --global --add diff.guitool kdiff3
$ git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add difftool.kdiff3.trustExitCode false
(Remember to replace the path with the actual path of Kdiff exe file.)
(请记住用Kdiff exe文件的实际路径替换路径。)
Then every time you come across a merge conflict you just need to run this command:
然后,每次遇到合并冲突时,您只需运行此命令:
$git mergetool
Then it opens the Kdiff3, and first tries to resolve the merge conflicts automatically. Most of the conflicts would be resolved spontaneously and you need to fix the rest manually.
然后它打开Kdiff3,并首先尝试自动解决合并冲突。大多数冲突将自发解决,您需要手动修复其余冲突。
Here's what Kdiff3 looks like:
这是Kdiff3的样子:
Then once you're done, save the file and it goes to the next file with conflict and you do the same thing again until all the conflicts are resolved.
然后,一旦完成,保存文件,它将转到下一个冲突的文件,然后再次执行相同的操作,直到所有冲突都得到解决。
To check if everything is merged successfully, just run the mergetool command again, you should get this result:
要检查是否所有内容都已成功合并,只需再次运行mergetool命令,您应该得到以下结果:
$git mergetool
No files need merging
7
This answers is to add an alternative for those VIM users like I that prefers to do everything within the editor.
这个答案是为那些喜欢在编辑器中做所有事情的VIM用户添加一个替代方案。
Tpope came up with this great plugin for VIM called fugitive. Once installed you can run :Gstatus
to check the files that have conflict and :Gdiff
to open Git in a 3 ways merge.
Tpope想出了一个名为逃犯的VIM插件。安装完成后,您可以运行:Gstatus来检查有冲突的文件,并且:Gdiff以3种方式合并打开Git。
Once in the 3-ways merge, fugitive will let you get the changes of any of the branches you are merging in the following fashion:
一旦进入3-way合并,fugitive将允许您以下列方式获取要合并的任何分支的更改:
:diffget //2
, get changes from original (HEAD) branch: :diffget // 2,从原始(HEAD)分支获取更改:
:diffget //3
, get changes from merging branch: :diffget // 3,从合并分支获取更改:
Once you are finished merging the file, type :Gwrite
in the merged buffer. Vimcasts released a great video explaining in detail this steps.
完成合并文件后,在合并缓冲区中键入:Gwrite。 Vimcasts发布了一个很棒的视频,详细解释了这一步骤。
3
git fetch
git checkout your branch
git rebase mastergit fetch git checkout你的分支git rebase master
In this step you will try to fix the conflict using your prefer IDE
在此步骤中,您将尝试使用您喜欢的IDE修复冲突
You can follow this link to check ho to fix the conflict in the file
https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/
您可以按照此链接检查以解决文件中的冲突问题https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/
git add
git rebase --continue
git commit --amend
git push origin HEAD:refs/drafts/master (push like a drafts)git add git rebase - 继续git commit --amend git push origin HEAD:refs / drafts / master(像草稿一样推送)
Now every thing is fine and you will find your commit in gerrit
现在一切都很好,你会发现你在gerrit中的承诺
I hope that this will help every one concerning this issue.
我希望这对每个人都有所帮助。
2
Try Visual Studio Code for editing if you aren't already. What it does is after you try merging(and land up in merge conflicts).VS code automatically detects the merge conflicts.
如果您还没有,请尝试使用Visual Studio代码进行编辑。它的作用是在您尝试合并(并在合并冲突中登陆)之后.VS代码自动检测合并冲突。
It can help you very well by showing what are the changes made to the original one and should you accept incoming
or
它可以很好地帮助你显示对原始的更改是什么,你应该接受传入或
current change
(meaning original one before merging)'?.
当前的变化(意思是合并之前的原始变化)'?
It helped for me and it can work for you too !
它对我有帮助,它也适合你!
PS: It will work only if you've configured git with with your code and Visual Studio Code.
PS:只有在使用代码和Visual Studio Code配置git时才会起作用。
1
For those who are using Visual Studio (2015 in my case)
对于那些使用Visual Studio的人(在我的情况下为2015)
Close your project in VS. Especially in big projects VS tends to freak out when merging using the UI.
在VS中关闭您的项目。特别是在大型项目中,VS在使用UI进行合并时往往会吓坏。
Do the merge in command prompt.
在命令提示符下执行合并。
git checkout target_branch
git checkout target_branch
git merge source_branch
git merge source_branch
Then open the project in VS and go to Team Explorer -> Branch. Now there is a message that says Merge is pending and conflicting files are listed right below the message.
然后在VS中打开项目并转到团队资源管理器 - >分支。现在有一条消息说Merge正在等待,并且消息下方列出了冲突的文件。
Click the conflicting file and you will have the option to Merge, Compare, Take Source, Take Target. The merge tool in VS is very easy to use.
单击冲突文件,您将可以选择合并,比较,获取源,获取目标。 VS中的合并工具非常易于使用。
1
If you are using intelliJ as IDE Try to merge parent to your branch by
如果您使用intelliJ作为IDE尝试将父级合并到您的分支
git checkout <localbranch>
git merge origin/<remotebranch>
It will show all conflicts like this
它将显示所有这样的冲突
A_MBPro:test anu$ git merge origin/ Auto-merging src/test/java/com/.../TestClass.java CONFLICT (content): Merge conflict in src/test/java/com/.../TestClass.java
A_MBPro:测试anu $ git merge origin /自动合并src / test / java / com /.../ TestClass.java CONFLICT(内容):在src / test / java / com /.../ TestClass.java中合并冲突
Now note that the file TestClass.java is shown in red in intelliJ Also git status will show
现在请注意,文件TestClass.java在intelliJ中显示为红色,同时git status将显示
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/test/java/com/.../TestClass.java
Open the file in intelliJ, it will have sections with
在intelliJ中打开文件,它将包含部分
<<<<<<< HEAD
public void testMethod() {
}
=======
public void testMethod() { ...
}
>>>>>>> origin/<remotebranch>
where HEAD is changes on your local branch and origin/ is changes from the remote branch. Here keep the stuff that you need and remove the stuff you don't need.After that the normal steps should do. That is
其中HEAD是您本地分支上的更改,而原始/是远程分支的更改。在这里保留你需要的东西,并删除你不需要的东西。之后,正常的步骤应该做。那是
git add TestClass.java
git commit -m "commit message"
git push
1
I follow the below process.
我按照以下流程。
The process to fix merge conflict:
修复合并冲突的过程:
First, pull the latest from the destination branch to which you want to merge git pull origin develop
首先,从你要合并的目标分支中拉出最新的git pull origin develop
As you get the latest from the destination, now resolve the conflict manually in IDE by deleting those extra characters.
当您从目标获取最新信息时,现在可以通过删除这些额外字符在IDE中手动解决冲突。
Do a git add
to add these edited files to the git queue so that it can be commit
and push
to the same branch you are working on.
做一个git add将这些编辑过的文件添加到git队列中,这样它就可以提交并推送到你正在处理的同一个分支。
As git add
is done, do a git commit
to commit the changes.
当git add完成后,执行git commit来提交更改。
Now push the changes to your working branch by git push origin HEAD
现在通过git push origin HEAD将更改推送到您的工作分支
This is it and you will see it resolved in your pull request if you are using Bitbucket or GitHub.
这就是它,如果您使用的是Bitbucket或GitHub,您将在拉取请求中看到它已解决。
0
A safer way to resolve conflicts is to use git-mediate (the common solutions suggested here are quite error prone imho).
解决冲突的一种更安全的方法是使用git-mediate(这里建议的常见解决方案非常容易出错)。
See this post for a quick intro on how to use it.
有关如何使用它的快速介绍,请参阅此文章。
0
git checkout branch1
git fetch origin
git rebase -p origin/mainbranch
If there are merge conflicts, fix them. Then, continue the rebase process by running: git rebase –-continue
如果存在合并冲突,请修复它们。然后,通过运行以下命令继续rebase进程:git rebase --continue
after the fixing you can commit and push your local branch to remote branch
修复后,您可以提交并将本地分支推送到远程分支
git push origin branch1
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2008/10/02/f34522167473b5a6a1f8bd9c6bdfc850.html。