git 如何从git存储库的历史记录中恢复整个目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9670745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:10:49  来源:igfitidea点击:

How to restore a whole directory from history of git repository?

git

提问by mostwanted

I would like to restore a whole directory (recursively) from the history of my git repository.

我想从我的 git 存储库的历史记录中(递归地)恢复整个目录。

There is only 1 branch (master).

只有 1 个分支(主)。

I know the commit where errors were included.

我知道包含错误的提交。

Can I use the sha1 hash of the parent commit to restore the state of the directory as it was before the errors were included?

我可以使用父提交的 sha1 哈希来恢复包含错误之前的目录状态吗?

I thought about something like this:

我想过这样的事情:

git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 path/to/the/folder/

but it did not work.

但它没有用。

回答by Carlos Campderrós

try adding '--' between revisions and paths:

尝试在修订和路径之间添加“--”:

git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 -- path/to/the/folder/ 

回答by Daniel Pittman

There are two easy ways to do this:

有两种简单的方法可以做到这一点:

If the commit that included the errors onlyincluded the errors, use git revertto invert the effects of it.

如果包含错误的提交包含错误,请使用git revert来反转它的效果。

If not, the easy path is this:

如果没有,简单的方法是这样的:

  1. git checkout 348…
  2. cp -a path/to/the/folder ../tmp-restore-folder
  3. git checkout HEAD # or whatever
  4. rm -rf path/to/the/folder
  5. mv ../tmp-restore-folder path/to/the/folder
  6. git add path/to/the/folder
  7. git commit -m "revert …"
  1. git checkout 348…
  2. cp -a path/to/the/folder ../tmp-restore-folder
  3. git checkout HEAD # or whatever
  4. rm -rf path/to/the/folder
  5. mv ../tmp-restore-folder path/to/the/folder
  6. git add path/to/the/folder
  7. git commit -m "revert …"

回答by Mark Fisher

If you simply do git checkout <SHA-ID>then it will temporarily move you to that sha-commit.

如果你只是这样做,git checkout <SHA-ID>那么它会暂时将你转移到那个 sha-commit。

Each commit object holds the entire structure of the disk at that time, so if you have files there and need to copy them out, you can do so. Warning though, you will not be in any branch, so you'll have to move back to master before copying the file into your working tree and commit it.

每个提交对象都保存了当时磁盘的整个结构,因此如果您在那里有文件并且需要将它们复制出来,您可以这样做。但是警告,您不会在任何分支中,因此在将文件复制到您的工作树并提交之前,您必须移回 master。