Friday, 19 April 2024

git cherry pick example

 

git cherry-pick is a Git command used to apply a specific commit from one branch to another. It's particularly useful when you want to pick individual commits from one branch and apply them to another branch without merging the entire branch.

Here's how you can use git cherry-pick:

  1. Identify the commit you want to apply to another branch. You can find the commit hash using git log.

  2. Switch to the branch where you want to apply the commit:

    bash
T
  • git checkout <target_branch>
  • Use git cherry-pick followed by the commit hash you want to apply:

    bash
  • git cherry-pick <commit_hash>
  • Resolve any merge conflicts if they occur. Git will pause the cherry-pick process if there are conflicts and allow you to resolve them manually.

  • Once conflicts are resolved, continue the cherry-pick process by running:

    bash
  • git cherry-pick --continue
  • Alternatively, you can abort the cherry-pick if you encounter issues:

    bash
    1. git cherry-pick --abort

    Here's an example:

    Suppose you have a branch featureA with a commit A that you want to apply to your main branch.

    1. Identify the commit hash of commit A:

      bash
  • git log
  • Switch to the main branch:

    bash
  • git checkout main
  • Cherry-pick commit A:

    bash
    1. git cherry-pick <commit_hash_of_A>
    2. Resolve conflicts if any.

    3. Continue or abort the cherry-pick process as needed.

    After the cherry-pick is complete, the changes from commit A will be applied to your main branch, allowing you to incorporate specific changes from one branch into another without merging the entire branch.

    No comments:

    Post a Comment