使用 git 在整个文件上“接受他们的"或“接受我的"的简单工具 [英] Simple tool to 'accept theirs' or 'accept mine' on a whole file using git
问题描述
我不想要一个可视化合并工具,我也不想在冲突的文件中使用 vi 并在 HEAD(我的)和导入的更改(他们的)之间手动选择.大多数时候,我要么想要他们的所有更改,要么想要我的所有更改.这通常是因为我的更改使其上流并通过拉取返回给我,但可能在不同地方略有修改.
I don't want a visual merge tool, and I also don't want to have to vi the conflicted file and manually choose the between HEAD (mine) and the imported change (theirs). Most of the time I either want all of their changes or all of mine. Commonly this is because my change made it upsteam and is coming back to me through a pull, but may be slightly modified in various places.
是否有命令行工具可以摆脱冲突标记并根据我的选择以一种或另一种方式进行选择?或者一组 git 命令,我可以用别名来做每一个.
Is there a command line tool which will get rid of the conflict markers and choose all one way or another based on my choice? Or a set of git commands which I can alias myself to do each one.
# accept mine
alias am="some_sequence;of;commands"
alias at="some_other_sequence;of;commands"
这样做很烦人.对于接受我的",我尝试过:
Doing this is rather annoying. For 'accept mine' I have tried:
randy@sabotage ~/linus $ git merge test-branch
Auto-merging Makefile
CONFLICT (content): Merge conflict in Makefile
Automatic merge failed; fix conflicts and then commit the result.
randy@sabotage ~/linus $ git checkout Makefile
error: path 'Makefile' is unmerged
andy@sabotage ~/linus $ git reset --hard HEAD Makefile
fatal: Cannot do hard reset with paths.
我应该如何摆脱这些更改标记?
How am I supposed to get rid of these change markers?
我能做到:
git reset HEAD Makefile; rm Makefile; git checkout Makefile
但这似乎有点绕,一定有更好的方法.在这一点上,我不确定 git 是否认为合并发生了,所以我认为这不一定有效.
But this seems rather round about, there must be a better way. And at this point, I'm not sure if git even thinks the merge happened, so I don't think this necessarily even works.
反过来说,接受他们的"同样是一团糟.我能弄清楚的唯一方法是:
Going the other way, doing 'accept theirs' is equally messy. The only way I can figure it out is do:
git show test-branch:Makefile > Makefile; git add Makefile;
这也给了我一个混乱的提交消息,其中包含两次 Conflicts: Makefile.
This also gives me a messed up commit message, which has Conflicts: Makefile in it twice.
有人可以指出如何以更简单的方式执行上述两个操作吗?谢谢
Can someone please point out how to do the above two actions in a simpler way? Thanks
推荐答案
解决方案很简单.git checkout
尝试从索引中检出文件,因此合并失败.
The solution is very simple. git checkout <filename>
tries to check out file from the index, and therefore fails on merge.
您需要做的是(即检查提交):
What you need to do is (i.e. checkout a commit):
要检查您自己的版本,您可以使用一个:
git checkout HEAD -- <filename>
或
git checkout --ours -- <filename>
(警告!:如果您正在重新调整 --ours
和 --theirs
的基础.)
(Warning!: If you are rebasing --ours
and --theirs
are swapped.)
或
git show :2:<filename> > <filename> # (stage 2 is ours)
要检查其他版本,您可以使用一个:
git checkout test-branch -- <filename>
或
git checkout --theirs -- <filename>
或
git show :3:<filename> > <filename> # (stage 3 is theirs)
您还需要运行add"以将其标记为已解决:
You would also need to run 'add' to mark it as resolved:
git add <filename>
这篇关于使用 git 在整个文件上“接受他们的"或“接受我的"的简单工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!