WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Intermediate · question 26 of 100

What is the purpose of ’git cherry-pick’, and how do you use it?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

In Git, the git cherry-pick command is used to apply the changes introduced by a specific commit to another branch. This can be useful when you want to selectively apply changes from one branch to another, without merging the entire branch.

Here’s how to use git cherry-pick:

Identify the commit that contains the changes you want to apply to another branch. You can use the git log command to view the commit history.

    # View the commit history
    git log

Checkout the branch that you want to apply the changes to.

    # Checkout the target branch
    git checkout <target-branch>

Use the git cherry-pick command followed by the commit hash of the commit you want to apply. This will apply the changes introduced by the commit to the current branch.

    # Cherry-pick a specific commit
    git cherry-pick <commit-hash>

Alternatively, you can cherry-pick a range of commits by specifying a range of commit hashes.

    # Cherry-pick a range of commits
    git cherry-pick <start-commit-hash>..<end-commit-hash>

Resolve any conflicts that may arise during the cherry-pick process. If the changes in the target branch conflict with the changes introduced by the cherry-picked commit, Git will pause the cherry-pick process and prompt you to resolve the conflicts manually.

    # Resolve conflicts
    git mergetool   # opens the mergetool to resolve conflicts
    git add <file>  # stage the resolved conflicts
    git cherry-pick --continue  # continue the cherry-pick process

Once all conflicts have been resolved, commit the changes using the git commit command.

    # Commit the changes
    git commit

In summary, the git cherry-pick command is used to apply the changes introduced by a specific commit to another branch. By using git cherry-pick, you can selectively apply changes from one branch to another, without merging the entire branch. This can be useful when you want to apply a bug fix or feature from one branch to another, or when you need to apply changes from a historical commit to a current branch.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics