Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert a folder to a particular commit by creating a patch

Tags:

git

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?

like image 924
hitfactory Avatar asked May 25 '11 02:05

hitfactory


People also ask

How do I revert a locally committed change?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.


2 Answers

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
like image 159
jamessan Avatar answered Oct 25 '22 17:10

jamessan


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
like image 24
Matthew Buckett Avatar answered Oct 25 '22 19:10

Matthew Buckett