问题
How do I check out a previous version of a directory under a new name?
(I have and always had a directory foo
in my repo. I want to create a directory foo_old
in my current working tree whose contents are those of HEAD~2:foo
. I need both versions at the same time, to make sure they give the same results, testing this requires quite a bit of code and requires having both results available at the same time (the results are not simply integers, say), so I cannot simply checkout HEAD~2
temporarily to do what I want)
This question asks how to check out a previous version of a file under a new name, but the accepted answer cannot be generalized to directories.
回答1:
You could do very simple:
git co -b new-branch-name # create and go to new branch, just to be sure
git slg # get the commit hash you need for next step
git co [commit-hash] # now you get the old folder by the state you need it
then copy the directory you need to your desktop. ( You are now in HEAD detached)
git co new-branch-name # cause you are in detached head go back to new-branch
On desktop rename the directory to foo_old
and copy paste it to where you need it in project.
回答2:
I just wrote a python script to copy a file or a sub-directory from Git history at specific revision.
The script can display the content on console or save under some directory. It preserves file execution permission and line ending.
The major logic in the script is:
- get list of files inside the directory at specific revision by
git ls-tree
- loop through the list
- read file content at revision by
git cat-file
- set file mode
- read file content at revision by
来源:https://stackoverflow.com/questions/46510629/git-checkout-old-version-of-directory-under-new-name