This study will illustrate the procedure to cherry-pick changes to a working copy without a commit on Git.
How to Cherry-Pick to Working Copy Without a Commit on Git?
To cherry-pick changes to a working copy without a commit, follow the given-provided instructions:
- Redirect to the particular directory.
- View branch content.
- Select the desired file and copy its commitβs hash value.
- Switch to the target branch.
- View target branch content and commit history.
- Cherry-pick changes without a commit using the βgit cherry-pick -n <SHA-hash>β command.
- Ensure changes.
Step 1: Navigate to Local Repository
First, write out the βcdβ command with a particular directory path and switch to it:
Step 2: View Branch Content
Then, list the available content of the current branch through the βlsβ command:
It can be seen that the repositoryβs βmasterβ branch contains two text files. Choose the desired file which needs to be copied without a commit to another branch through cherry-pick. For instance, we have selected the βFile1.txtβ file:
Step 3: View Git Log
Next, execute the provided command to view the commit history of the current working branch:
From the below-given output, we have copied the β627d33cβ commit id of the desired file which was previously selected:
Step 4: Check Available Branches
Now, list the available branches in the current repository by typing out the βgit branchβ command:
According to the following output, the repository contains βfeatureβ and βmasterβ two Git branches and the βmasterβ branch is the current working branch:
Step 5: Switch to Target Branch
Utilize the below-stated command along with the target branch name and switch to it:
Step 6: View Target Branch Content
Next, view the content of the current working branch:
It can be observed that the current βfeatureβ branch contains only one text file:
Step 7: View Commit History
Check the Git log to view the commit history including the βfeatureβ branchβs current HEAD position:
In the given-provided output, it can be seen that the HEAD is pointing to the below highlighted βbf80309β commit hash:
Step 8: Cherry-pick Without Commit
Now, write out the βgit cherry-pickβ command along with the β-nβ flag and desired commit id to cherry-pick changes without a commit:
Here, the β-nβ flag is used for not including the commit:
Step 9: Display Updated Content
After that, list the updated content of the current working branch using the βlsβ command:
The below screenshot indicates that the selected file has been copied from the βmasterβ branch to the βfeatureβ branch:
Step 10: Verify Changes
Lastly, check the commit history to view the current position of HEAD:
According to the below-provided image, the HEAD is still pointing to the same previous commit which indicates that the new changes have been cherry-picked without the commit:
We have provided the easiest way to cherry-pick changes to a working copy without a commit.
Conclusion
To cherry-pick changes to the working copy without a commit, first, switch to the particular repository. Then, choose the desired file and copy its commitβs SHA hash value. Next, switch to another branch and execute the βgit cherry-pick -n <SHA-hash>β command to cherry-pick changes without a commit. Lastly, verify the changes by viewing the commit history. This study has explained how to cherry-pick to working copy without a commit on Git.