Home > Software design >  How I cherry pick previous branch's latest commit by one command?
How I cherry pick previous branch's latest commit by one command?

Time:12-15

For example, I have branch "feature-A". Then I execute one commond that can help me checkout to "feature-B" and cherry pick "feature-A"'s lastest's commit.

Does git have such a magical command? Or does anyone have a ready-made script? If so, this could save me a lot of time. Thanks!

CodePudding user response:

You can concatenate 2 git commands: first to move to your desired branch (in this case feature-B) and then cherry pick the last commit on the top of your other branch (in this case feature-A)

You can do it by running git checkout feature-B && git cherry-pick feature-A

Remember you can specify -n after cherry-pick if you don't want to commit (so you can check it before committing), or on the other hand you can concatenate && git push in case you don't need to check but you want directly to push everything in the same command after the cherry pick.

CodePudding user response:

Below bash/zsh script might help if you want to cherry-pick only the latest commit from feature-A

git checkout feature-A
commitId=$(git log -n 1 --pretty=format:"%h")
git checkout feature-B
git cherry-pick $commitId
git diff --stat --cached origin/feature-B

Then to push the changes, run below command

git push origin feature-B
  • Related