Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git-cherry-pick only changes to certain files?

If I want to merge into a Git branch the changes made only to some of the files changed in a particular commit which includes changes to multiple files, how can this be achieved?

Suppose the Git commit called stuff has changes to files A, B, C, and D but I want to merge only stuff's changes to files A and B. It sounds like a job for git cherry-pick but cherry-pick only knows how to merge entire commits, not a subset of the files.

like image 846
Tobias Kienzler Avatar asked Apr 19 '11 13:04

Tobias Kienzler


People also ask

How cherry pick partial commit?

To use it in combination with cherry-pick: git cherry-pick -n <commit> # get your patch, but don't commit (-n = --no-commit) git reset # unstage the changes from the cherry-picked commit git add -p # make all your choices (add the changes you do want) git commit # make the commit!

How do you commit cherry pick changes?

To change the commit message when cherry-picking, use “git cherry-pick” with the “-e” option. As illustrated in this example, your default editor will open and it will let you change the commit message. When you are satisfied with the edits, save your file and your commit message should be saved successfully.


1 Answers

I'd do it with cherry-pick -n (--no-commit) which lets you inspect (and modify) the result before committing:

git cherry-pick -n <commit>  # unstage modifications you don't want to keep, and remove the # modifications from the work tree as well. # this does work recursively! git checkout HEAD <path>  # commit; the message will have been stored for you by cherry-pick git commit 

If the vast majority of modifications are things you don't want, instead of checking out individual paths (the middle step), you could reset everything back, then add in what you want:

# unstage everything git reset HEAD  # stage the modifications you do want git add <path>  # make the work tree match the index # (do this from the top level of the repo) git checkout . 
like image 110
Cascabel Avatar answered Oct 15 '22 07:10

Cascabel