2

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.

Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
kerner1000
  • 3,382
  • 1
  • 37
  • 57

2 Answers2

2
# 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)

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
2

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.

Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32