Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 807
nyarlathotep108 Avatar asked Mar 15 '16 12:03

nyarlathotep108


People also ask

Do we need to commit after cherry pick?

Cherry picking in Git means to choose a commit from one branch and apply it onto another. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch. Make sure you are on the branch you want to apply the commit to.


2 Answers

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
like image 192
Enrico Campidoglio Avatar answered Oct 21 '22 12:10

Enrico Campidoglio


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>
like image 28
Igal S. Avatar answered Oct 21 '22 11:10

Igal S.