5

Consider the following tree:

A --- B --- C --- D --- E --- F --- master \ \ B' --- C' --- D' --- topic 

where (B != B'). I would like to do git rebase --onto master master topic but this generates conflicts. But the situation is simpler: I would like to put the single topic commit onto master.

git checkout master git cherry-pick topic git checkout topic git reset --hard master git checkout master git reset --hard HEAD~1 

Isn't it possible to do with a single command the commands above?

3
  • Why not just fix the conflicts ? Commented Apr 12, 2015 at 11:31
  • because actually there are no conflicts... Commented Apr 12, 2015 at 11:37
  • 1
    Possible duplicate of git rebase a single commit Commented May 4, 2017 at 15:33

1 Answer 1

4

Reset your branch first, then use the reflog to find the commit to cherry pick:

git checkout -B topic master # re-create topic branch at the commit of master git cherry-pick topic@{1} # copy the old tip of the topic branch 

Another – maybe even simpler – way would be to pass rebase a range of commits which only consists of the single commit you want to have rebased:

git rebase --onto master topic^ topic 
Sign up to request clarification or add additional context in comments.

6 Comments

if I understand the -B flag correctly, the second cherry-pick should say that there's nothing to do, because it try to pick the same commit master is pointing to, I think topic@{1} should be replaced by the direct hash of the commit, correct me if I'm wrong.
@MohammadAbuShady: topic@{1} refers to the previous commit the branch pointed to. Before the checkout/reset it pointed at D', after the checkout it points at F. topic@{0} would be F, topic@{1} would be D' and topic@{2} would be whatever commit pointed to before D' (most probably C'
hm, this is interesting, so If for example i do a git rebase origin/develop then I find that I messed up a conflict or something, I could simple do a git reset --hard develop{1} ?
@MohammadAbuShady: develop@{1}, but yes. Use git reflog $branch to view the reflog a specific branch. If called without arguments, it will show the HEAD reflog.
ah ok, so @{1} is the history index of the commit in the reflog, thank you for the tip, (or more like the pointer history)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.