开发者

git: store different stages of a file in the index/working tree after conflict

开发者 https://www.devze.com 2023-01-23 14:38 出处:网络
Assume one file is in conflict state with git. It means that the index contains 3 versions of the file, the previous one, the one from one branch, the one from the 开发者_运维技巧other branch.

Assume one file is in conflict state with git.

It means that the index contains 3 versions of the file, the previous one, the one from one branch, the one from the 开发者_运维技巧other branch.

I would like to checkout version 3 ("theirs") in the working directory, and stage version 2 ("ours") in the index.

Is there a painless way to do that?


Probably the most pain-free way is like this.

# 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

Fiddling around with the index with the update-index plumbing shouldn't be necessary unless you've lost your HEAD (so to speak).


The trick is in "adding one version to the index": that would mark the file as solved (meaning no more "ours" or "theirs").
So it needs to be the last step.

Maybe something like:

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

Not exactly "painless"...


To avoid the temporary file, Jakub Narębski suggests using git update index (a plumbing command) to manipulate directly the index.

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

--cacheinfo is used to register a file that is not in the current working directory. This is useful for minimum-checkout merging.

By default, when a file path exists in the index, git update-index refuses an attempt to add path/file. Similarly if a file path/file exists, a file path cannot be added. With --replace flag, existing entries that conflict with the entry being added are automatically removed with warning messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消