1

I cherry-pick a commit from another person's branch. Later the user updated his commit(maybe by squash), so the commit SHA changed. How do I cherry-pick to get latest commit?

2
  • You could git reset your repository to before you cherry-picked the earlier version of the commit, then just cherry-pick the new version. Commented Feb 12, 2022 at 2:57
  • yeah that's a workaround. I'm wondering if there is way that doesn't require rebase, drop commit. Commented Feb 12, 2022 at 3:06

1 Answer 1

1

Like most things in Git, there are many different ways to accomplish this. Yet there are multiple factors that could change the answer, including the current state of your branch, what you wish your branch to look like afterwards.

Based on larsks' suggestion:

You could git reset your repository to before you cherry-picked the earlier version of the commit, then just cherry-pick the new version.

And your response:

yeah that's a workaround. I'm wondering if there is way that doesn't require rebase, drop commit.

I'm going to assume you have more commits after the commit you already cherry-picked, otherwise you would simply do larsks' suggestion as written. In this case perhaps the simplest solution is to revert the original and repick the new one:

git revert <commit-id-on-your-branch-of-original-cherry-picked-commit> git cherry-pick <new-id-of-commit-on-other-branch> 

Now you might be done, or if you're willing to rewrite your branch you could interactive rebase and squash those 2 new commits up into the previous cherry-picked commit. This would preserve your original order.

Along the same lines, but without requiring an interactive rebase, you could also do:

# Assumptions: # Your current branch: "my-branch" # Original cherry-picked commit on your branch: <picked-commit-id> # New commit to cherry-pick from other branch <new-commit-id-to-pick> # setup git switch my-branch # checkout your branch git branch my-branch-copy # make a temporary copy of your branch # rebuild your branch git reset --hard <picked-commit-id>~1 # reset to commit before the picked commit git cherry-pick <new-commit-id-to-pick> git cherry-pick <picked-commit-id>..my-branch-copy # rewrite your branch 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.