Git "revert" current directory
Asked Answered
K

4

51

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?

Kessinger answered 2/1, 2013 at 16:17 Comment(3)
Use git checkout instead of reset.Aubrey
git checkout <branchname>~1 -- path/to/directory/you/want/updated should do the trick. The ~1 after the branch name means back one commit.Sulfonmethane
it does not delete new files within workin dir as SVN would do, can someone confirm that?Pyrone
F
88

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).

Ferocious answered 2/1, 2013 at 16:23 Comment(1)
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/Bane
E
43

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.

Ephrem answered 5/2, 2017 at 8:49 Comment(1)
This work great if your change are not already added in your next commit.Shiloh
P
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
Phyte answered 25/2, 2016 at 15:26 Comment(3)
@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.Phyte
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.Guaranty
@Guaranty DonePhyte
E
1

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

git checkout -- ./*
git checkout -- mydir/*
Eruption answered 23/11, 2020 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.