In this blog, we will learn the method to use git cherry-pick a commit with an example. So, let’s start!
Example: How to git cherry-pick a Commit?
To git cherry-pick a commit, first, create a new Git local directory and move to it. Next, initialize and modify the file using t. Then, update the second file and commit changes to the repository. Next, remove all existing content from the directory. Add all changes, commit modifications to the Git repository, and view the reference log history. Finally, execute the “$ git cherry-pick <commit-reflog>” command on the terminal.
Now, try out the below-provided instructions step by step!
Step 1: Create Directory
First, create a new Git directory using the below-stated command:
Step 2: Move Inside Directory
To move to the newly created directory, execute the “cd” command:
Step 3: Initialize the Git Directory
Then, execute the below-provided command to initialize the created Git directory:
Step 4: Modify File
Add some text in the file to update it with the help of the “echo” command and the redirect operator “>”:
Step 5: Add Changes to Git Repository
Next, execute the “git add” command to add all added changes into the repository:
Step 6: Commit Changes
Run the “git commit” command with the commit message to update the repository:
Step 7: Update File
Create another file and then update it:
Step 8: Add All Modifications
Add all made changes into the local repository:
Step 9: Commit Changes
Now, commit changes with a commit message using the “-m” option:
Step 10: Remove All Files
Upon doing so, execute the “rm” command to remove all created files from the repository:
Here, the asterisk “*” symbol indicates all files with specified extension:
Step 11: List Repository Content
Now, verify the deleted operation using the “ls” command to view the list of repository content:
As you can see, the repository is empty which indicates that the operation is performed successfully:
Step 12: Add Changes
Next, add all made changes to the staging area and update it:
Step 13: Commit Changes
After that, commit modifications to the repository by exciting the “git commit” command with any commit message:
Step 14: Check Reference Log History
Now, display the reference log history of Git repository:
Here, we will select the commit reference log which we want to commit with cherry-pick:
Step 15: cherry-pick Commit
Next, execute the “cherry-pick” command to commit with copied commit reference log:
Step 16: Verify cherry-pick Commit Operation
Lastly, verify the committed cherry-pick commit operation by utilizing the “git reflog .” command:
As you can see, the same commit is committed with the cherry-pick commit:
We have briefly explained the method to git cherry-pick a commit with the help of an example.
Conclusion
To git cherry-pick a commit, first, create a new Git directory and initialize it. Next, modify the file using the “echo <add-text> > <file-name>” command. Then, update the second file and commit changes to the repository. Remove all existing files from the repository using the “rm” command. Track all changes, commit modifications and view the reference log history. Lastly, execute the “$ git cherry-pick <commit-reflog>” command to commit with the selected commit. This blog explained how to git cherry-pick a commit.