Git

How to Abort a Cherry-pick on Git?

Cherry-picking in Git means copying some commits from one Git local branch and applying them to another. Sometimes, while working on a team project, developers want to apply changes from one branch to another instead of merging the whole branch content. For this corresponding purpose, they perform cherry-pick operations.

This post will explain the method to abort a cherry-pick on Git.

How to Abort a Cherry-pick on Git?

When developers cherry-pick commits on Git, they often encounter conflict due to some reasons. To resolve this conflict, it is required to abort the cherry-pick operation using the “git cherry-pick –abort” command.

So, first, we will perform cherry-pick operations and show the conflict. Then, we will demonstrate how to abort a cherry-puck on Git.

Step 1: Navigate to a Particular Directory

Use the below-provided command along with the specific path and switch to it:

$ cd "C:\Git\Repo3"

 
Step 2: View Commit History

Then, check the Git log to display the commit history:

$ git log --oneline

 
It can be seen that the HEAD of the current branch is pointing to the “ab1bc8e” commit id. Select the SHA-hash of a particular commit for cherry-picking purposes. For instance, we have chosen the “0b0e67e” commit id:


Step 3: View List of Branches

Next, view the list of the available branches in the working repository:

$ git branch

 
The below-screenshot indicates that the repository contains “alpha” and “master” local branches. The asterisk “*” symbol before the “master” branch indicates that it is the current local working branch. From the given output, choose a target branch:


Step 4: Switch to Another Branch

Execute the “git switch” command along with the previously selected target branch and switch to it:

$ git switch alpha

 

Step 5: Cherry-pick Commit

Next, cherry-pick the target commit by running the below-given command along with its SHA-hash:

$ git cherry-pick 0b0e67e

 
It can be seen that the provided commit could not be cherry-picked, and a conflict occurred due to some reasons:


Now, follow the below-provided steps to resolve this conflict.

Step 6: Abort Cherry-pick Commit

To abort the cherry-picked commit, execute the below-provided command:

$ git cherry-pick --abort

 
Here, the “–abort” option is used to undo the cherry-pick operation:


We have successfully explained the process to abort a cherry-pick on Git.

Conclusion

Developers often perform cherry-pick operations to apply the changes of one branch to another. But sometimes, they encounter conflicts. So, use the “git cherry-pick –abort” command to abort the cherry-pick in order to resolve the conflict. This post has explained the procedure to abort a cherry-pick 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.