ademartini
ademartini

Reputation: 1441

Git - How to revert entire directory to specific commit (removing any added files)

I want revert a directory in git - reverting all files inside, as well as removing any files added since that commit. Doing a checkout only seems to satisfy my first requirement, but doesn't delete any files.

Upvotes: 33

Views: 27614

Answers (5)

James Mudd
James Mudd

Reputation: 2393

I think you can do this in one step using

git checkout --no-overlay <ref> -- dir/to/revert

--no-overlay https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt---no-overlay will remove files not at ref

Upvotes: 1

Andrew Linck
Andrew Linck

Reputation: 61

The top answer is good but I would like to simplify it.

git rm -r /path/to/dir
git checkout <rev> /path/to/dir       <-- the <rev> is the git commit id
git commit -m "reverting directory"

Everything is exactly the same except the checkout command in my case was:

git checkout 70157c4f57aa21307cd96f146f8e98b808e1aada ./WebRoot

I am within a directory called Memento on the command line and I use a dot to denote that the new directory which the git commit info will go into will be in the Memento folder.

I was checking out whatever was in that commit to that folder.

Upvotes: 5

ademartini
ademartini

Reputation: 1441

I figured out the simplest solution.

git rm /path/to/dir
git checkout <rev> /path/to/dir
git commit -m "reverting directory"

Then delete any untracked files.

git rm

Remove files from the working tree and from the index https://git-scm.com/docs/git-rm

git checkout 

Updates files in the working tree to match the version in the index or the specified tree. https://www.git-scm.com/docs/git-checkout

git commit

Record changes to the repository https://www.git-scm.com/docs/git-commit

Upvotes: 57

sunfffd
sunfffd

Reputation: 151

remove only the folder and its content on git

git rm -r --cached myFolder

remove folder on git and locally

git rm -r myFolder

then commit and push again

To Revert to a previous commit

#reset to previous commit, replace with your commit hash code, you can find it from your commit history 
git reset {commit hash} 

#moves pointer back to previous head branch
git reset --soft HEAD@{1}

git commit -m "Reverted commit to blah"

#update your working copy
git reset --hard

Reverting to part of a commit In that case you need to revert to a particular commit and add patch

#reset to previous commit, but don't commit the changes
$ git revert --no-commit {last commit hash}   

# unstage the changes
$ git reset HEAD .             

# add/remove stuff here
$ git add file
$ git rm -r myfolder/somefiles          

# commit the changes  
$ git commit -m "fixed something"

# check the files
$ git status

#discard unwanted changes
$ git reset --hard             

Upvotes: 10

Andrew C
Andrew C

Reputation: 14863

To restore so that it matches what it looked like at

First nuke the directory from the staging area

git rm --cached -r <directory>

Now read in the treeish for just that directory

git read-tree <old SHA>^{tree}:<directory> --prefix=<directory>

Then commit

git commit -m "reverting <directory>"

Then just throw out working directory changes that are leftover.

Upvotes: 1

Related Questions