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.
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. One alternative to get a clean working tree that is only the contents of <commit>
would be this:
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...
None of these will actually move your HEAD
, which I think is the effect you are trying to achieve...
只是不要传递文件名。
git checkout COMMIT-HASH
I believe this SO question may be of use: 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 --no-commit-id --name-only -r COMMIT_ID
Once you have that, you can feed it to 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).
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
. Perhaps git diff
would be better in that case:
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:如何列出给定提交中的所有文件?
下一篇: Git检出所有来自确切提交的文件