How to revert committed changes to files of a certain type in git

Viewed 70

I would like to revert an older git known commit (not the last one, already pushed to upstream), but would like to affect only certain file types, e.g. *.java. Other changes, such as changes to files of type *.xml I would like to keep.

Any downstream changes to *.java files should be undone as well.

2 Answers
# first revert your commit but without committing
git revert -n <commitToRevert>

# then restore your .xml files
git checkout <commitToRevert> -- *.xml

# and finally commit
git commit

(revert without committing)

If you know the commitid you want to revert from, you could checkout the known files from the parent commit (commitid^). In your case the files are *.java:

# checkout *.java files recursively from a parent commit of a known "commitid"
git checkout commitid^ -- `git ls-tree commitid -r --name-only | grep ".java"`

# Commit
git commit

A parent commit is denoted by a caret (^) symbol after the commit hash.

Also see similar answer. Also see checkout by wildcard.

Related