我如何将修改的文件转换为以前的修订,在一个特定的 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./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 checkout HEAD^1 path/to/file
或
git checkout HEAD~1 path/to/file
然后,只是步行,并采取“新”版本。
武装着一个承诺可以有两个父母在合并的情况下,你应该知道,头1是第一个父母,头1是第二个父母。
或者它会工作,如果树上只有一个父母。
这对我工作:
git checkout <commit hash> file
接下来的改变:
git commit -a
有趣的是, git checkout foo 如果工作副本在一个名为 foo 的目录中工作,则不会工作;但是, git checkout HEAD foo 和 git checkout./foo 都会:
$ pwd
/Users/aaron/Documents/work/foo
$ git checkout foo
D foo
Already on "foo"
$ git checkout ./foo
$ git checkout HEAD foo
git checkout -- foo
這將重新安裝 foo 到 HEAD. 您也可以:
git checkout HEAD^ foo
一次重新审查等。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别