134

Here's my history for the folder 'somefolder'

$ git log somefolder
commit 89cd
More changes to somefolder
commit ef47a
Updating somefolder and other stuff
commit e095
Bugs fixed in somefolder

I want to revert somefolder back to the 'Bugs fixed in some folder" commit.

Since the second commit involved changes outside of somefolder, I don't want to revert this commit.

I guess the safest way would be to create a diff/patch between commit e095 and 89cd that applies just to some folder, and then apply that patch. How can I do that?

Melebius
6,7924 gold badges45 silver badges55 bronze badges
asked May 25, 2011 at 2:54

3 Answers 3

209

You can use git checkout to update your repository to a specific state.

git checkout e095 -- somefolder

As for your question about generating the diff, that would work too. Just generate the diff to go from your current state back to e095:

git diff 89cd..e095 -- somefolder
answered May 25, 2011 at 3:10
Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't remove files that were added in that commit, use git reset e095 -- some/folder instead
From git 2.22 you can use --no-overlay for git checkout so that tracked files that are not in commit you are checking out from will be removed.
87

You can use git reset to reset the index which will also include removing files that were added in more recent commits (git checkout on it's own doesn't do this):

git reset e095 -- somefolder

However git reset doesn't update the working copy and the --hard option doesn't work with folders. So then use git checkout to make the working copy the same as the index:

git checkout -- somefolder

and then if you also want to remove any files added you also need todo:

git clean -fd somefolder
answered Feb 14, 2017 at 10:46

3 Comments

this answer works for me when the directory include new files.
This answer is much better if you really want a clean snapshot of the repository at that time.
Much much better indeed !
5

The git restore command is the command designed restore files and directories to the state they were in a different commit. If you specify a directory to restore, it will also remove files that were not present in the commit you are restoring from.

To restore the content of directory somefolder to the state it was in commit e095, you can run:

git restore -s e095 somefolder

By default, git restore restores content to the state it was in HEAD, but by using the -s switch (short for --source), we can restore content to the state it was in any commit.

answered Mar 6, 2024 at 11:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.