2

As mentioned in the title, I want to perform git revert on a commit that made some undesirable changes but only in a certain folder/directory.

asked Oct 11, 2019 at 3:27

2 Answers 2

3

With Git 2.23 (August 2019) and the new command git restore, plus the : pathspec signature:

git restore -s@~ -SW -- :path/to/folder/**

Long form:

git restore --source @~ --staged --worktree -- :path/to/folder/**

Check the result with git status, then commit.


The OP adds:

I will need to do a git reset of the files I do not want to keep changes of, before doing git checkout on them.
Doing only git checkout does not work because after git revert, the modified files are in the staging directory.

Yes, that is why git checkout is confusing, and is in the process of being replaced by:

No need for revert+reset+checkout: if you want to restore files... use git restore.

answered Oct 11, 2019 at 4:51
Sign up to request clarification or add additional context in comments.

Comments

2

What about:

  1. git revert --no-commit
  2. git checkout everything except your folder
  3. git commit

Edit: For git >=2.23 please see VonC's answer.

answered Oct 11, 2019 at 3:34

4 Comments

This worked (almost) perfectly for me! But I will need to do a git reset of the files I do not want to keep changes of, before doing git checkout on them. Doing only git checkout does not work because after git revert, the modified files are in the staging directory, while git checkout only works on unstaged files.
@Samson Which is why I proposed git restore: it will reset the working tree and the staging area.
@VonC I see, will try that next time, thank you so much!
@Samson If you are still with a Git older than Git 2.23, you have chosen the right solution. With Git 2.23+, the same solution is needlessly confusing.

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.