有没有办法让 Git 将文件标记为冲突?

发布于 2024-08-31 04:46:55 字数 74 浏览 5 评论 0原文

可以提交包含冲突数据的文件。有没有办法再次将这些文件标记为冲突,以便运行 git mergetool 将生成必要的文件并运行合并工具?

It's possible to commit files that contains conflict data. Is there a way to mark these files as conflicted again, so that running git mergetool will generate the necessary files and run the merge tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落叶缤纷 2024-09-07 04:46:56

最优雅的解决方案是从一开始就防止这个问题:
git config --global mergetool.[工具].cmd [命令行调用]

git config --global mergetool.[tool].trustExitCode false

The most elegant solution would be to prevent this problem from the very beginning:
git config --global mergetool.[tool].cmd [command-line call]

git config --global mergetool.[tool].trustExitCode false

套路撩心 2024-09-07 04:46:56

据我所知,当文件中仍包含冲突标记时,您将无法提交。...这并不完全正确:
OP提到你可以(我在这里复制他的过去的bin),但这对于合并工具来说还不够再次触发:

Auto-merged README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ git add README
lynx:~/test_clone$ git commit -a
Created commit 46ee062: It works!
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ cat README
<<<<<<< HEAD:README
testingtesting
=======
hmm
>>>>>>> 881d60f5f738bc5716f5c9a9384e262b535717fd:README
lynx:~/test_clone$

正如Charles Bailey评论,并在此SO 答案查询 mergetool 是因为索引中有同一个文件的 3 个实例

对于冲突中未合并的文件,git 在索引中提供该文件的公共基础版本、本地版本和远程版本。 (这是 git mergetool 在 3 向 diff 工具中读取它们的位置。)您可以使用 git show 来查看它们:

# common base:
git show :1:afile.txt

# 'ours'
git show :2:afile.txt

# 'theirs'
git show :3:afile.txt

git add (使用任何内容(包括冲突标记)将自动删除其中 2 个,确保mergetool不会再次被调用。

As far as I know, you won't be able to commit while a file still contain conflict markers in it.... which is not exactly true:
The OP mentions that you can (I copy here his pastbin), but that won't be enough for the mergetool to be trigger again:

Auto-merged README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ git add README
lynx:~/test_clone$ git commit -a
Created commit 46ee062: It works!
lynx:~/test_clone$ ls
README
lynx:~/test_clone$ cat README
<<<<<<< HEAD:README
testingtesting
=======
hmm
>>>>>>> 881d60f5f738bc5716f5c9a9384e262b535717fd:README
lynx:~/test_clone$

As Charles Bailey comments, and illustrates in this SO answer, the mergetool is queried because there are 3 instances of the same file in the index:

For an unmerged file in a conflict git makes available the common base, local and remote versions of the file in the index. (This is where they are read from for use in a 3-way diff tool by git mergetool.) You can use git show to view them:

# common base:
git show :1:afile.txt

# 'ours'
git show :2:afile.txt

# 'theirs'
git show :3:afile.txt

git add (with whatever content, including conflict markers) will automatically removes 2 of them, ensuring the mergetool won't be called again.

这个俗人 2024-09-07 04:46:56

请使用 git update-index --unresolve

自从 git 1.7 起,它使用索引中的解析撤消信息来恢复所有 3 个阶段(1:base、2:ours、3:theirs): https://github.com/git/git/commit/8aa38563b22c84b06ea1fff9638cc1f44fda726f

Please use git update-index --unresolve

Since git 1.7 it uses resolve-undo info from the index to restore all 3 stages (1:base, 2:ours, 3:theirs): https://github.com/git/git/commit/8aa38563b22c84b06ea1fff9638cc1f44fda726f

一枫情书 2024-09-07 04:46:55

如果索引已经处于冲突状态,只需使用 --conflict=merge 标志检查文件:

git checkout --conflict=merge file

如果索引是干净的,因为已[错误地]添加了未解析的文件,只需重置它检查之前:

git reset file
git checkout --conflict=merge file

这将允许您正常恢复冲突解决(例如,git mergetool)。

注意:应 @fourpastmidnight 的要求,将对 @jakub-narębski 的答案的评论提升为自己的答案。 :)

If the index is already in a conflict state, simply check out the file with the --conflict=merge flag:

git checkout --conflict=merge file

If the index is clean because the unresolved file has been [erroneously] added, just reset it before checking it out:

git reset file
git checkout --conflict=merge file

This will allow you to resume conflict resolution normally (e.g., git mergetool).

NOTE: Promoting a comment to @jakub-narębski's answer into its own answer by request from @fourpastmidnight. :)

节枝 2024-09-07 04:46:55

您可以使用 git checkout --conflict=merge -- file 获取带有冲突标记的文件内容,但如果您已使用 git add file 清理了索引(或者如果GUI 为你做了这件事)这是行不通的。

有 git update-index --unresolve ,但它很hacky,并且工作得不太可靠。我认为它恢复的状态对于 git-mergetool 来说是不够的。

您可能需要重做合并,或者使用 git update-index --cacheinfo 手动设置阶段版本... git-stash 可以帮助您保留正确解决的冲突。

You can get contents of file with conflict markers using git checkout --conflict=merge -- file, but if you have cleaned up index by using git add file (or if GUI did that for you) it wouldn't work.

There is git update-index --unresolve, but it is hacky, and does not work very reliably. I think the state it restores would be not enough for git-mergetool.

You would probably have to redo merge, or use git update-index --cacheinfo to manually set stages version... git-stash can help you preserve correctly resolved conflicts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文