48

In Subversion (SVN), it's possible to do svn revert ./* in which the current directory and only the current directory gets reverted.

What is the Git equivalent to svn revert in which only the current directory gets reverted?

I know that there's git reset --hard, but it reverts everything and not just the current directory.

How would I revert just the current directory in Git?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pillarOfLight
  • 8,592
  • 15
  • 60
  • 90

4 Answers4

86

Like vcsjones says, the solution here is git checkout:

git checkout <refspec> -- path/to/directory  # or path/to/file

where <refspec> can, for instance, be HEAD, that is, the current working commit. Note that this usage of the checkout command will affect the working tree but not the index.

git revert is used to "revert a commit", and by this, it should not be understood that the commit disappears from the tree (it would play havoc with history -- if you want that, look at git rebase -i). A reverted commit consists of applying, in reverse, all changes from the commit given as an argument to the tree and create a new commit with the changes (with a default commit message, which you can modify).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fge
  • 119,121
  • 33
  • 254
  • 329
  • Doesn't work if you added files in your current branch (and if those files have unique and different names). Within my branch, I had to delete the folder and then checkout the folder again: `rm folder -rf && git checkout origin/master folder/` – k0pernikus Dec 01 '20 at 14:09
41

Go to the folder you want to revert and do this:

git checkout -- .

See more in krlmlr's answer to How to git reset --hard a subdirectory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
7

When I was a Git novice (and afraid of the terminal) I found the easiest way was to:

  • switch to the branch you want to revert your specific subdirectory to
  • copy the subdirectory you want to revert to your desktop
  • switch back to your branch
  • overwrite the subdirectory you want to replace in your Git directory with the one you copied to your desktop
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Spence
  • 7,999
  • 3
  • 39
  • 63
  • 4
    @saeraphin Perhaps I've misunderstood something, but how does this not work? When I was a git novice (and probably still am) this worked perfectly and is easy to understand. I don't use subversion and do not know what "svn revert" does but to revert a sub directory to that of another branch or another commit, this works. It's also not scary for new people using the terminal. – David Spence Sep 30 '16 at 12:32
  • okay, I misread one word. I still find the command line more efficient though. I will gladly remove the down vote if you're kind enough to replace "noobs" by something less offensive. – saeraphin Oct 04 '16 at 08:58
  • @saeraphin Done – David Spence Oct 04 '16 at 11:04
0

If you want to do it recursively from a specific directory:

git checkout -- ./*
git checkout -- mydir/*
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wisbucky
  • 33,218
  • 10
  • 150
  • 101