Git: Checkout old version of directory under new name
Asked Answered
N

2

2

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.

Nicolanicolai answered 1/10, 2017 at 8:34 Comment(0)
G
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.

Gomez answered 1/10, 2017 at 8:45 Comment(1)
I guess I was overcomplicating thingsNicolanicolai
C
0

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
Catrinacatriona answered 12/6, 2020 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.