0 votes
1 view
in DevOps and Agile by (20k points)

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

1 Answer

0 votes
by (28.4k points)

To inspect the result before committing use the following command:

$ git cherry-pick -n <commit>

# now unstage and remove the unwanted modifications from the work tree as well.

$ git checkout HEAD <path>

# now commit

$ git commit

But in case, 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:

# to unstage everything

$ git reset HEAD

# to stage the modifications you do want

$ git add <path>

# to make the work tree match the index

# (do this from the top level of the repo)

$ git checkout .

Welcome to Intellipaat Community. Get your technical queries answered by top developers !