简体   繁体   中英

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

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, eg *.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.

# 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM