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


当前回答

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

git checkout [commit-ref] [filename]

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

其他回答

执行下列命令,进行软重新设置,变更到您的本地。

git reset --soft HEAD^1

查看您此前承诺的文件的状态,到达您的本地,然后进行更改。

 git status

承诺并按下文件后进行更改. 未来的承诺历史错误的文件将不会显示

在从起源中提取之前,始终保持喷射(可选步骤)

 git remote prune origin

这里是我的路。

在 Android Studio 中,打开文件。

(b) git -> 显示历史,找到我要转向的以前的承诺。

c) git checkout commit_id file_path

但是,请注意, 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 修订之前。

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 <commit hash> file

接下来的改变:

git commit -a