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


当前回答

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 文件名称

假设你想要的承诺的哈希是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 足够新,你可以使用这个命令,但文档来了一个警告:

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

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

对我来说,没有一个答案看起来很清楚,所以我想添加我的,看起来很容易。

我有一个承诺 abc1 之后我做了几个(或一个修改)到一个文件 file.txt。

1.git checkout file.txt : 这将删除本地变更,如果你不需要它们

2.git checkout abc1 file.txt : 这将使您的文件到您想要的版本

3.git commit -m “Restored file.txt to version abc1” : 这将承诺您的转换。

git push : 这将把一切推到远程存储库中

在步骤2和3之间,当然,你可以做 git 状态来了解正在发生的事情。 通常你应该看到 file.txt 已经添加了,这就是为什么不需要添加 git。

您可以使用任何引用到一个 git 承诺,包括 SHA-1 如果这是最方便的。

git checkout [commit-ref] -- [文件名]