BmyGuest
BmyGuest

Reputation: 2949

How to restore a single subfolder to specific date without affecting others using Git?

I have a Git repository which includes a whole folder structure, and I want o reset one of them (not all) to the state it was in at a particular time, but I do not want to affect any of the other files/folders of the repository.

How do I achive this with Git?

Say I have Git repository in my root/ folder that contains:

root/
root/FolderA/
root/FolderB/sub1/
root/FolderB/sub2/
root/FolderC/

I want to restore root/FolderB/ and all of its contents to the situation it was on 1st January 2018, but I do not want to change the content of the other folders.

Note, I'm generally using a UI frontent (Git Extensions), so I'm not regularly using GIT from a command line. Neither am I very proficient in GIT in general.

Upvotes: 1

Views: 42

Answers (1)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

This command will restore the version of root/FolderB/ from the last commit before the 2nd of January 2018:

git checkout $(git rev-list -n 1 --before="2018-01-02 00:00" master)^ -- root/FolderB/

Explanation:

  • git rev-list -n 1 --before="2018-01-02 00:00" master: get the hash of the last commit before the 2nd of January 2018
  • git checkout <commit-hash>^ master -- root/FolderB/: restore the root/FolderB/ from <commit-hash> on the master branch

Upvotes: 2

Related Questions