简体   繁体   English

Git从精确提交中检出所有文件

[英]Git checkout all files from exact commit

As I know, to get old version of some file I need input:据我所知,要获取某些文件的旧版本,我需要输入:

git checkout COMMIT-HASH -- file-name.ext

But if I wont to get all modified files from some commit?但是,如果我不想从某个提交中获取所有修改过的文件? Can I input something like:我可以输入以下内容:

git checkout COMMIT-HASH -- *

PS I know that I can list files manually, but it's not convenient if there are many files. PS我知道我可以手动列出文件,但是如果文件很多就不方便了。

If I understand what you are asking correctly, I think one of these two will do the job:如果我理解您的要求是正确的,我认为这两个中的一个可以完成这项工作:

git reset --merge <commit>

or或者

git checkout <commit> -- .

If there are files currently in your working directory that are not in <commit> , they will still be there, which might confuse things, depending on exactly why you want to do this.如果您的工作目录中当前存在不在<commit> ,它们仍然会在那里,这可能会造成混淆,具体取决于您为什么要这样做。 One alternative to get a clean working tree that is only the contents of <commit> would be this:获得只有<commit>内容的干净工作树的另一种方法是:

rm -r *
git archive --format=tar <commit> | tar xf -

For that matter, you could just empty your worktree before the git reset or git checkout commands above, as well...就此而言,您也可以在上面的git resetgit checkout命令之前清空您的工作树……

None of these will actually move your HEAD , which I think is the effect you are trying to achieve...这些都不会真正移动你的HEAD ,我认为这是你想要达到的效果......

I believe this SO question may be of use: How to list all the files in a commit?我相信这个 SO 问题可能有用: How to list all the files in a commit?

Basically, to get a listing of the files, you can use git diff-tree :基本上,要获取文件列表,您可以使用git diff-tree

git diff-tree --no-commit-id --name-only -r COMMIT_ID

Once you have that, you can feed it to git checkout:一旦你有了它,你就可以将它提供给 git checkout:

git diff-tree --no-commit-id --name-only -r COMMIT_ID | \
    xargs git checkout COMMIT_ID --

So, the git diff-tree will help get the list of files, and the xargs git checkout COMMIT_ID -- will help reset the affected files back to the state they were in at that commit (you're only rolling back those particular files).因此, git diff-tree将帮助获取文件列表,而xargs git checkout COMMIT_ID --将帮助将受影响的文件重置回它们在该提交时所处的状态(您只是回滚那些特定文件) .

You probably need to do this at the top of your working tree.您可能需要在工作树的顶部执行此操作。 Also, I didn't try seeing what would happen if there's a rename involved, so there is likely another edge case you might need to consider.此外,我没有尝试查看如果涉及重命名会发生什么,因此您可能需要考虑另一个边缘情况。 Spaces in path names could be a problem here too, but I didn't see a way to get the paths in a null-terminated format with git diff-tree .路径名中的空格在这里也可能是一个问题,但我没有看到使用git diff-tree以空终止格式获取路径的方法。 Perhaps git diff would be better in that case:在这种情况下,也许git diff会更好:

git diff --name-only -z COMMIT_ID^ COMMIT_ID | \
    xargs -0 git checkout COMMIT_ID --

Also, if you really want to go back in that way and there's really only one commit that's the issue, perhaps git revert would be a better way to go about it.此外,如果您真的想以这种方式返回并且确实只有一个提交就是问题,那么git revert可能是解决它的更好方法。

如何在没有提交的情况下挑选樱桃

git cherry-pick COMMIT-HASH --no-commit

根本不要传递文件名。

git checkout COMMIT-HASH 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM