| by Arround The Web | No comments

How to git cherry-pick a Commit Example

Sometimes, you want to make changes and include them in the current working branch. In order to perform this action, it is required to utilize the “git cherry-pick” command. It is very feasible and widely used to perform specified operations. More specifically, this command represents picking a commit from one branch and applying it to another Git branch. This works opposite to the other approaches, such as combining and rebase, which normally apply multiple commits to another branch.

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:

$ mkdir demo12

Step 2: Move Inside Directory

To move to the newly created directory, execute the “cd” command:

$ cd demo12

Step 3: Initialize the Git Directory

Then, execute the below-provided command to initialize the created Git directory:

$ git init

Step 4: Modify File

Add some text in the file to update it with the help of the “echo” command and the redirect operator “>”:

$ echo "file1">file1.txt

Step 5: Add Changes to Git Repository

Next, execute the “git add” command to add all added changes into the repository:

$ git add .

Step 6: Commit Changes

Run the “git commit” command with the commit message to update the repository:

$ git commit -m "first file added"

Step 7: Update File

Create another file and then update it:

$ echo "file2">file2.txt

Step 8: Add All Modifications

Add all made changes into the local repository:

$ git add .

Step 9: Commit Changes

Now, commit changes with a commit message using the “-m” option:

$ git commit -m "second file added"

Step 10: Remove All Files

Upon doing so, execute the “rm” command to remove all created files from the repository:

$ rm *.txt

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:

$ ls

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:

$ git add .

Step 13: Commit Changes

After that, commit modifications to the repository by exciting the “git commit” command with any commit message:

$ git commit -m "2 files deleted"

Step 14: Check Reference Log History

Now, display the reference log history of Git repository:

$ git reflog .

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:

$ git cherry-pick 87d1e54

Step 16: Verify cherry-pick Commit Operation

Lastly, verify the committed cherry-pick commit operation by utilizing the “git reflog .” command:

$ git reflog .

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.

Share Button

Source: linuxhint.com

Leave a Reply