Git

How to Git Cherry-pick Only Changes to Certain Files?

While working on Git, developers create multiple files on different branches. Sometimes, they may want to merge certain files of the specific commit from one branch to another target branch. For this purpose, the cherry-pick operation can be performed. However, a single commit can contain more than one file. So, when we perform cherry-pick, it will merge all files of that particular commit.

This study will explain the method to Git cherry-pick changes/modifications to certain files.

How to Git Cherry-pick Only Changes/Modifications to Certain Files?

To cherry-pick only changes to certain files, try out the given-provided instructions:

    • Redirect to the local directory.
    • View branch content.
    • Choose the desired file and copy its commit’s hash value.
    • Switch to the target branch.
    • Cherry-pick changes using the β€œgit cherry-pick -n <SHA-hash>” command.
    • Unstage all files through the β€œgit reset HEAD” command.
    • Stage desired files.
    • Commit changes.

Step 1: Switch to Local Directory

First, enter the β€œcd” command and redirect to the local repository:

$ cd "C:\Git\new_Repo"

 
Step 2: View Branch Content

Next, display the content of the current working branch:

$ ls

 
It can be observed that the β€œmaster” branch contains some text files. Select the desired file whose changes need to be merged to another branch. For instance, we have chosen the β€œT2.txt” file:


Step 3: View Git Log

Then, run the below-provided command to view the commit history of the current branch:

$ git log --oneline

 
From the given-provided output, we have copied the β€œ3598cc5” commit id of the selected file:


Step 4: Switch to Target Branch

Redirect to the target branch by running the following command along with the target branch name:

$ git switch beta

 

Step 5: Perform Cherry-pick Operation

Now, type out the β€œgit cherry-pick” command with the β€œ-n” option and desired commit id to cherry-pick file changes without a commit:

$ git cherry-pick -n 3598cc5

 

Step 6: Verify Changes

View the content of the β€œbeta” branch to view new changes:

$ ls

 
It can be observed that the desired commit contained three files, so all three files of the β€œmaster” branch had been copied to the β€œbeta” branch:


Note: As we need only the changes of β€œT2.txt” files, so now we will unstaged other files and keep our desired file only.

Step 7: Unstaged Files

Next, run the provided command to unstaged all files from the current branch:

$ git reset HEAD

 

The below output indicates that all three files have been unstaged:

$ git status

 

Step 8: Add Desired File to Staging Area

Then, stage only the desired file using the β€œgit add” command:

$ git add T2.txt

 

Step 9: Verify Changes

Next, view the current status of the working branch to view changes:

$ git status

 
It can be observed that the desired β€œT2.txt” file has been staged:


Step 10: Commit Changes

Finally, commit the desired changes using the given-below command:

$ git commit -m "T2.txt file added"

 

We have provided the easiest method to Git cherry-pick only modifications to certain files.

Conclusion

To Git cherry-pick only modifications to certain files, first, redirect to the local repository. Then, select the desired file and copy its commit’s SHA hash value. After that, switch to the target branch and run the β€œgit cherry-pick -n <SHA-hash>” command to cherry-pick changes. Next, utilize the β€œgit reset HEAD” command to stage everything and add only the desired files to the Git staging area. Lastly, commit new changes. This study has explained how to cherry-pick only changes/modifications to certain files in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.