74

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
16.1k34 gold badges122 silver badges219 bronze badges
asked Jun 15, 2015 at 11:39
1

7 Answers 7

109

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

answered Jun 15, 2015 at 11:43
Sign up to request clarification or add additional context in comments.

6 Comments

I went to the folder (for which I want to revert all changes) and typed git checkout ./
It only lists the changes, but does not actually pull them, use git checkout -f to force checkout
Why does this have so many upvotes? this doesnt do anything for a folder full of moved files/directories
This is not working, please update your answer
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.
|
16

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.

answered Apr 14, 2023 at 17:12

3 Comments

I think this assumes all files in given directory should be removed from GIT.
Thanks @TheHumanCat, I've improved my answer with your feedback.
That was handy. Thank you!
4

I achieved what I wanted with this

git checkout <branch> <path>
answered Mar 11, 2024 at 13:49

Comments

3
  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

answered Aug 24, 2022 at 7:56

Comments

3

One crucial detail here is -- before the path. So the correct answer is

git checkout -- app/code/core/
answered Dec 7, 2023 at 15:53

2 Comments

How -- is useful here?
2

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

It applies recursively to subfolders as well.

answered Mar 7, 2023 at 17:17

1 Comment

This works well but needs to be combined with git clean -fxd folder-with-changes/ to also clean up new, not previously committed files.
-5

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

answered Mar 15, 2021 at 7:42

1 Comment

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 .

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.