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


当前回答

显然,有人需要在 git 上写一本可理解的书,或者 git 需要在文档中更好地解释。

cd <working copy>
git revert master

他们会放弃最后的承诺,似乎正在做。

伊恩

其他回答

这是一个非常简单的步骤. 检查文件到我们想要的承诺ID,这里一个承诺ID之前,然后只是 git承诺修改,我们已经完成了。

# git checkout <previous commit_id> <file_name>
# git commit --amend

如果我们想把任何文件带到承诺的顶部的任何先前承诺 ID,我们可以轻松地做到。

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

假设你想要的承诺的哈希是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 checkout./foo 和 git checkout HEAD./foo 并不完全相同; 案例在点:

$ echo A > foo
$ git add foo
$ git commit -m 'A' foo
Created commit a1f085f: A
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 foo
$ echo B >> foo
$ git add foo
$ echo C >> foo
$ cat foo
A
B
C
$ git checkout ./foo
$ cat foo
A
B
$ git checkout HEAD ./foo
$ cat foo
A

(第二个添加步骤是指数中的文件,但它不会被承诺。

Git checkout./foo 是指从指数中转向./foo 路径; 添加 HEAD 指示 Git 将指数中的路径转向其 HEAD 修订之前。

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

我有一个承诺 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。