我如何将修改的文件转换为以前的修订,在一个特定的 commit hash (我通过 git log 和 git diff 确定)?


当前回答

git checkout -- foo

這將重新安裝 foo 到 HEAD. 您也可以:

git checkout HEAD^ foo

一次重新审查等。

其他回答

假设你想要的承诺的哈希是c5f567:

git checkout c5f567 -- file1/to/restore file2/to/restore

git checkout man 页面提供更多信息。

如果你想在 c5f567 之前返回承诺,添加 ~1 (在 1 是你想返回的承诺的数量,它可以是任何东西):

git checkout c5f567~1 -- file1/to/restore file2/to/restore

作为一个侧面注意事项,我一直对这个命令不舒服,因为它用于普通事物(分支之间的变化)和不寻常的破坏性事物(工作目录的变化)。


有一个新的 git 恢复命令,它是专门设计的恢复工作复制文件已被修改。 如果你的 git 足够新,你可以使用这个命令,但文档来了一个警告:

这个命令是实验性的,受害者可能会改变。

要转到文件的先前命令版本,请获取命令号,然后说EB917a1

git checkout eb917a1 YourFileName

如果你只需要回到最后的承诺版本

git reset HEAD YourFileName
git checkout YourFileName

这只会带你到文件的最后一个承诺状态。

git log --oneline  // you see commits, find commit hash to which you want reset
git diff y0urhash src/main/.../../YourFile.java   // to see difference
git reset y0urhash src/main/.../../YourFile.java   // revert to y0urhash commit
git status                                        // check files to commit
git commit -m "your commit message"
git push origin

将文件转换为一个特定的承诺

git checkout Last_Stable_commit_Number - 文件名

2. 将文件转换为一个特定的分支

git checkout branchName_Which_Has_stable_Commit 文件名称

我现在有相同的问题,我发现这个答案是最容易理解的(commit-ref 是您想要返回日志中的变更的 SHA 值):

git checkout [commit-ref] [filename]

这将把旧版本放在你的工作目录中,从那里你可以承诺它,如果你愿意。