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/.
-
1You could also stash specific directory if you want... with that trick: stackoverflow.com/a/13941132/338581noisy– noisy2015年06月15日 11:57:38 +00:00Commented Jun 15, 2015 at 11:57
7 Answers 7
If you want to undo the changes, do git checkout app/code/core/
6 Comments
git checkout -f to force checkoutAs 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.
3 Comments
I achieved what I wanted with this
git checkout <branch> <path>
Comments
- cd to the folder you want to remove, eg.
cd app/code/core/ - 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
Comments
One crucial detail here is -- before the path.
So the correct answer is
git checkout -- app/code/core/
2 Comments
-- is useful here?If the changes aren't committed, you can run git restore folder-with-changes/*
It applies recursively to subfolders as well.
1 Comment
git clean -fxd folder-with-changes/ to also clean up new, not previously committed files.For me git checkout -f did the trick, git checkout just lists changes without applying them
1 Comment
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 .