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.
2 Answers 2
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 resetof the files I do not want to keep changes of, before doinggit checkouton them.
Doing onlygit checkoutdoes not work because aftergit 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:
git switch(for branches only)git restore(for files only)
No need for revert+reset+checkout: if you want to restore files... use git restore.
Comments
What about:
git revert --no-commit- git checkout everything except your folder
- git commit
Edit: For git >=2.23 please see VonC's answer.
4 Comments
git restore: it will reset the working tree and the staging area.