我想做一个提交的还原,但仅限于某些文件.(不是结账;还原.如果您不熟悉差异,请继续阅读.)
我试过这个
git revert --no-commit abcdef123456 -- my/path/to/revert
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
fatal: ambiguous argument 'my/path/to/revert': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
Run Code Online (Sandbox Code Playgroud)
但这正是我所做的!(是的,my/path/to/revert是在我工作的树上.)
我的工作理论是,不可能仅还原某些文件,并且Git错误消息具有误导性.
(Git 1.7.9.5)
这不是一个重复的还原一个文件到git的以前的版本.
vcs*_*nes 20
我不认为git允许您指定要还原的特定文件.我能想到的最好的是:
git revert --no-commit <commit hash> # Revert, don't commit it yet
git reset # Unstage everything
git add yourFilesToRevert # Add the file to revert
git commit -m "commit message"
git reset --hard # Undo changes from the part of the revert that we didn't commit
Run Code Online (Sandbox Code Playgroud)
vcsjones的答案可能是最好的方法,因为revert使用三向合并机制.但是,对于许多情况,您可以git apply -R(相反)修补相关文件(或文件),例如:
git show <rev> -- path/to/file | git apply -R
Run Code Online (Sandbox Code Playgroud)
(或git diff-any diff生成器,允许您将结果限制为特定文件,但git show实际上是非合并提交的明显要点;对于合并,您需要指定正确的父级).
一个较短的序列,您可以列出您想要的简短列表:
git revert that_commit # do the whole revert
git reset --hard HEAD^ # in what turns out to have been a throwaway commit
git checkout HEAD@{1} -- one/folder # and just take what you want of the results
Run Code Online (Sandbox Code Playgroud)