Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently reset subdirectory to specific past state

Tags:

git

In a git repository I have a subdirectory that I want to reset to a specific state in the past (an earlier commit). Is it possible to undo all commits on files in a specific subdirectory? I think there are no commits that apply changes to files within this subdirectory and on files located elsewhere at the same time. But it would be nice if it were possible to leave commits on files unaffected that are not located in this directory?

I don't need to remove the history. It would be no problem to checkout the old state, remove the current content of the subdirectory, and copy and commit the old state in its place. But eventually there may be a more elegant (gitonic) way.

like image 580
moooeeeep Avatar asked May 15 '12 08:05

moooeeeep


People also ask

Does git reset hard remove unstaged files?

git reset --hard is a classic command in this situation - but it will only discard changes in tracked files (i.e. files that already are under version control). To get rid of new / untracked files, you'll have to use git clean !

How do I get rid of all changes in working directory?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.


2 Answers

The simplest way to do it to use the ability of git checkout to be applied on a subdirectory. Then stage and commit:

git checkout <old-SHA1> -- path/to/subdir
git add -A path/to/subdir
git commit -m "reverted files in path/to/subdir to what it was at <old-SHA1>"
like image 171
CharlesB Avatar answered Oct 05 '22 19:10

CharlesB


If you work out all of the commits that you want to undo, you can use git revert to back them out - this would be most useful if there was only one or two commits that were the troublemakers.

CharlesB's answer is a simpler approach to take if there are lots of commits, or the commits touch other parts of the project (but you could actually work around that by using the -n (don't autocommit) flag, and only committing the changes you are interested in)

like image 35
Cebjyre Avatar answered Oct 05 '22 20:10

Cebjyre