简体   繁体   中英

How to cherry-pick only certain file type from a commit

I'm looking for a way to just select certain file types from a commit. Example If we have 10 files (5->.java+ 5->.png). I want to select only .png files changes/files from the commit. How can I achieve this.

Git cherry-pick applies an entire commit from some other branch or location to your current branch. If instead you want to reset a handful of files to some other commit, then use this form of git checkout :

git checkout abcd1234 -- path/to/file1.java path/to/file2.png   # etc.

where abcd1234 is the SHA-1 hash of the commit from which you want to take the Java and PNG files. This will update these selected files in your working directory, but won't stage or commit them.

Edit:

To checkout all PNG files at a certain path, change directories to the folder where all the files are located, and then use:

git checkout abcd1234 -- "*.png"

From Git base command:

git cherry-pick -n <commitedHashCode>

Example:

git cherry-pick -n 54f1a843aece872dda792bd8c036994bbee08434

Note: -n is indicating cherry-pic code will not commit in your local branch. It will bring all the changes into your local branch without commit.

Now you have the option to delete all .java file(or whatever you want to keep) from your local branch then do the commit and push action.

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