我最近提交了一个文件到我的分支的头部,其中有错误。我需要做以下事情:
最好的办法是什么?
你自己也说过:
首先从一次提交中取回文件,然后:
$> git checkout HEAD~1 path/to/file.ext
那就承认吧:
$> git commit -a -m 'Retrieved file from older revision'
如果只对上次提交时出现的文件进行更改,您甚至可以使用 git-revert:
git-revert
$> git revert HEAD
我认为最好把这个作为一个单独的提交,因为它会准确地告诉你恢复了什么,以及为什么。但是,您可以通过使用 --amend开关到 git-commit将其压缩到以前的提交中。
--amend
git-commit
在这种情况下,要小心:
Commit hash - File modified aaaaaaa index.php bbbbbbb test.php ccccccc index.php
Git checkout HEAD ~ 1(or HEAD ^) index.php 尝试将 index.php 文件签出到以前的 HEAD 散列(bbbbbb) ,但这不是真正的以前的提交散列文件 ccccc。在前面的 HEAD 散列中,index.php 仍然保持不变,因为最后一次更改是在 hash cccccc 中进行的。
若要将某个文件还原为以前影响该文件的提交散列,请使用:
git log -n 2 --pretty=format:%h path/to/file.ext
忽略第一个散列并采用第二个散列,然后:
git checkout <second_hash> path/to/file.ext git commit -m 'Revert this file to real previous commit'