我想要从我的git存储库的历史记录中恢复一个整个目录(递归)。
只有1个分支(主分支)。
我知道包括错误的提交。
我可以使用父提交的sha1哈希来将目录状态恢复到在包括错误之前的状态吗?
我考虑了像这样的东西:
git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 path/to/the/folder/
但是它没有起作用。
我想要从我的git存储库的历史记录中恢复一个整个目录(递归)。
只有1个分支(主分支)。
我知道包括错误的提交。
我可以使用父提交的sha1哈希来将目录状态恢复到在包括错误之前的状态吗?
我考虑了像这样的东西:
git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 path/to/the/folder/
但是它没有起作用。
尝试在修订版本和路径之间添加“--”:
git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 -- path/to/the/folder/
如果你想从以前的提交中恢复一个目录,你可以将提交哈希替换为HEAD~1,例如:
git checkout HEAD~1 -- path/to/the/folder/
rm -Rf path/to/the/folder
,然后执行了 git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 -- path/to/the/folder/
- 结果:该路径中的文件与该提交时完全相同,没有任何差异和额外的文件被创建。这正是我想要的。 - therobyouknow--
是什么作用? - Ray Foss对于现代git(-s
或--source
):
git restore -s commit-sha-that-contains-dir relative/path/to/folder
man 参考手册:
-s <tree>, --source=<tree>
Restore the working tree files with the content from the given tree. It is common to specify
the source tree by naming a commit, branch or tag associated with it.
If not specified, the contents are restored from HEAD if --staged is given, otherwise from the index.
As a special case, you may use "A...B" as a shortcut for the merge base of A and B if there
is exactly one merge base. You can leave out at most one of A and B, in which case it
defaults to HEAD.
git revert
来反转其影响。git checkout 348…
cp -a path/to/the/folder ../tmp-restore-folder
git checkout HEAD # 或其他分支
rm -rf path/to/the/folder
mv ../tmp-restore-folder path/to/the/folder
git add path/to/the/folder
git commit -m "revert …"
git checkout -- path/to/folder/
您可以使用git checkout master -- path/to/the/folder-you-want-to-restore/
命令从master
分支恢复本地分支上的目录或文件。
如果你只是执行git checkout <SHA-ID>
,那么它会暂时将你移动到该SHA-commit。
每个提交对象都保存了那个时间磁盘的完整结构,因此如果你在那里有文件并且需要将其复制出来,可以这样做。但要注意,你不会处于任何分支上,所以你必须在将文件复制到你的工作树并提交之前回到主分支。
git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 -- path/to/the/folder/
- Carlos Campderrós