Git

How to Cherry-Pick to Working Copy Without Commit on Git?

While working on a software development project, developers work on several branches that have a lot of commits. Sometimes, they want to copy or apply some changes from one Git branch to another target branch. However, they do not want to have the old branch’s commits into their target branch. In this situation, Git commands allow users to apply changes to another branch without creating a new commit.

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:

$ cd "C:Git\Repos1"

Step 2: View Branch Content

Then, list the available content of the current branch through the “ls” command:

$ ls

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:

$ git log --oneline

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:

$ git branch

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:

$ git switch feature

Step 6: View Target Branch Content

Next, view the content of the current working branch:

$ ls

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:

$ git log --oneline

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:

$ git cherry-pick -n 627d33c

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:

$ ls

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:

$ git log --oneline

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.

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.