Home > database >  git: restore folder and all subfolders/files under a different name
git: restore folder and all subfolders/files under a different name

Time:12-01

On the master branch, I have new version of the folder with files in it, e.g.:

config
  - db
    - rdbms
      - postgres.conf
      - redis.conf
    - nosql
      - mongo.conf
  - web
    - apache.conf
  - security.conf

I need to take older version of these files and add them to the master but the top folder config should be renamed to config-old, so that both config and config-old are present:

config
  - db
    - rdbms
      - postgres.conf
      - redis.conf
    - nosql
      - mongo.conf
  - web
    - apache.conf
  - security.conf

config-old
  - db
    - rdbms
      - postgres.conf
      - redis.conf
    - nosql
      - mongo.conf
  - web
    - apache.conf
    - nginx.conf

I know how to use git show or git cat-file to restore individual files and using shell redirection I can save them under another name, but how do I do this with an entire folder and all subfolder/subfiles in it?

Obviously, I can just switch to the required branch/revision, copy the folder somewhere outside of the git root, switch back to master and then copy back under different name, but I wonder if this can be done with git itself?

CodePudding user response:

You can do it quite easily with basic commands:

git checkout $OLD_VERSION
cp -R config config-old
git checkout master

At this point config-old is untracked so:

git add config-old
git commit -m 'Copy config from $OLD_VERSION to config-old'

CodePudding user response:

If you create an empty folder config-old beforehand,
you can use git archive to get config/ from the revision of your choice
and extract it to the folder config-old/ like this:

git archive --format=tar <COMMIT/BRANCH/TAG> -- config | tar --extract --directory=./config-old --strip-components=1
  •  Tags:  
  • git
  • Related