nyarlathotep108
nyarlathotep108

Reputation: 5521

Git cherry pick a commit and place it below

From what I know, by default the cherry-pick command takes a commit and places it on top of the current branch. Is it possible to cherry-pick a commit in Git and place it below the current commit?

Upvotes: 11

Views: 10260

Answers (2)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59963

Here's another approach. Let's say your history looks like this:

A - B - D master, HEAD
    \
     C other

and you want to cherry pick commit C before HEAD so that the resulting history becomes:

A - B - C' - D' master, HEAD
    \
     C other

Then you can do:

  1. git checkout HEAD^ to move HEAD to commit B
  2. git cherry-pick other master to apply commits C and D on top of B
  3. git branch -f master HEAD to make master point to the same commit as HEAD
  4. git checkout master to move HEAD to master

Upvotes: 4

Igal S.
Igal S.

Reputation: 14583

You can always perform a rebase after the cherry-pick. So it would look something like that:

git cherry-pick <hash>
git rebase HEAD~2 -i

Swap the commit orders in the rebase window.

Second Option

In case you want to solve conflicts only once, as you stated. You can take the long way. Remove the current commit, cherry pick the other one, and then cherry-pick the latest commit.

git log --oneline -1
  <write_down_this_hash> description
git reset --hard HEAD~1
git cherry-pick <hash to cherry pick>
git cherry-pick <write_down_this_hash>

Upvotes: 18

Related Questions