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:
Step 2: View Branch Content
Next, display the content of the current working branch:
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:
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:
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:
Step 6: Verify Changes
View the content of the “beta” branch to view new changes:
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:
The below output indicates that all three files have been unstaged:
Step 8: Add Desired File to Staging Area
Then, stage only the desired file using the “git add” command:
Step 9: Verify Changes
Next, view the current status of the working branch to view changes:
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:
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.