54

How I can undo changes made in a specific folder?

I've modified files from multiple folders like

app/code/core/
app/code/local/
app/design/frontend/

I want to undo all the changes made in files present in app/code/core/, while keeping the changes modified in files present in app/code/local/ and app/design/frontend/.

nbro
  • 15,395
  • 32
  • 113
  • 196
amitshree
  • 2,048
  • 2
  • 23
  • 41
  • 1
    You could also stash specific directory if you want... with that trick: http://stackoverflow.com/a/13941132/338581 – noisy Jun 15 '15 at 11:57

5 Answers5

89

If you want to undo the changes, do git checkout app/code/core/

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • 4
    I went to the folder (for which I want to revert all changes) and typed git checkout ./ – Gopalkrishna Mudaliar Aug 02 '18 at 01:00
  • 1
    It only lists the changes, but does not actually pull them, use `git checkout -f` to force checkout – Aziris Mar 15 '21 at 07:38
  • 10
    Why does this have so many upvotes? this doesnt do anything for a folder full of moved files/directories – searchengine27 Oct 12 '21 at 16:25
  • 1
    This is not working, please update your answer – Mohamed Mo Kawsara Jun 13 '22 at 11:21
  • This answer is TERRIBLE. In simple cases it will do the right thing, and in advanced cases it will do the wrong thing but give the appearance of working. This is because git reasons about files, not directories, and in the case of checkout git will only update files that existed in the code being checked out. Therefore, you need to first delete all files in the directory. See my answer below. – tenedor Apr 14 '23 at 17:14
  • @searchengine27, undoing a moved folder is a different action in git from undoing changes to the contents of a folder. Git treats a move as a delete plus an add. You need to remove the new folder, then git checkout the old folder. – tenedor Apr 14 '23 at 17:16
2
  1. cd to the folder you want to remove, eg. cd app/code/core/
  2. Run git clean -fd

NOTE: You can also run a dry run using git clean -fdn to see what file will be removed by the commands

Sam Kah Chiin
  • 4,375
  • 5
  • 25
  • 30
2

If the changes aren't committed, you can run git restore folder-with-changes/*

It applies recursively to subfolders as well.

shloosh
  • 525
  • 7
  • 10
  • This works well but needs to be combined with `git clean -fxd folder-with-changes/` to also clean up new, not previously committed files. – Martin_W Apr 30 '23 at 08:26
2

As of git 2.22+:

To undo uncommitted changes to a directory:

git checkout --no-overlay -- <directory>

To reset a directory to its state as of a specific commit:

git checkout --no-overlay <commit> -- <directory>

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


Prior to git 2.22:

WARNING: These methods will also delete git-ignored files from the directory.

To undo uncommitted changes to a directory:

rm -rf <directory>
git checkout -- <directory>

To reset a directory to its state as of a specific commit:

rm -rf <directory>
git checkout <commit> -- <directory>

Note: git checkout <directory> by itself is a terrible answer. In simple cases it will do the right thing, and in advanced cases it will do the wrong thing but give the appearance of working. This is because git reasons about files, not directories, and in the case of checkout git will only update files that existed in the code being checked out. Therefore you need to use the --no-overlay flag (2.22+) or need to first delete all files in that directory.

tenedor
  • 671
  • 7
  • 15
-5

For me git checkout -f did the trick, git checkout just lists changes without applying them

Aziris
  • 157
  • 2
  • 9
  • 3
    Be careful with this -- it will undo ALL changes in your entire repo, rather than in a single directory. The reason `git checkout` didn't work for you is that you have to specify a directory with it. For example (for the current directory and all subdirectories): `git checkout .` – Kevin Meboe Jun 17 '21 at 18:53