5

假设一个文件与 git 处于冲突状态。

这意味着索引包含文件的 3 个版本,前一个版本,来自一个分支的版本,来自另一个分支的版本。

我想在工作目录中检出版本 3(“他们的”),并在索引中暂存版本 2(“我们的”)。

有没有一种无痛的方法来做到这一点?

4

2 回答 2

4

大概最无痛的方式就是这样。

# Checkout 'their' version from the index into the working tree
git checkout --theirs file

# reset the index to the HEAD version, i.e 'our' branch
git reset HEAD file

update-index除非您丢失了HEAD(可以这么说),否则没有必要用管道摆弄索引。

于 2010-11-03T21:15:25.677 回答
1

诀窍在于“向索引添加一个版本”:这会将文件标记为已解决(意味着不再是“我们的”或“他们的”)。
所以它必须是最后一步。

也许是这样的:

git show :3:<filename> > <filename>.theirs # save the "theirs" version in a tmp file
git show :2:<filename> > <filename>        # display the "ours" version in the working dir 
git add <filename>                         # add it to the index
                                           #  (marking the conflicting file as resolved)
move  <filename>.theirs  <filename>        # erasing the file with the "theirs" version
                                           # in the working directory

不完全是“无痛”...


为了避免临时文件,Jakub Narębski建议使用git update index(管道命令)直接操作索引。

--replace --cacheinfo <mode> <object> <path> 

--cacheinfo用于注册不在当前工作目录中的文件。这对于最小结帐合并很有用。

默认情况下,当索引中存在文件路径时, git update-index 拒绝尝试添加path/file. 同样,如果文件path/file存在,则无法添加文件路径。使用--replace标志,与正在添加的条目冲突的现有条目将自动删除并显示警告消息。

于 2010-11-03T18:58:42.200 回答