Rebase a single commit to HEAD

597 views Asked by At

I would like to rebase a given commit to be HEAD of my current branch. Of course I can use git rebase -i <future-head-SHA>~ and then move it down to HEAD (as described in this question), but I want to automate this process.

Is there a neat way to do this?

Thanks

2

There are 2 answers

1
Jan Krüger On BEST ANSWER

Not really. Interactive rebase is the only tool designed to do this kind of thing. You could, hypothetically, script this, though:

  • Write a script that reads the list of rebase -i instructions from the file passed as its first argument - the file you'd normally get to edit yourself in an editor -, and writes a re-ordered list back into that file.
  • Tell Git to use that script as an editor when starting the interactive rebase, so that it's used to rewrite the rebase instructions instead of letting you edit them manually.

The following should work, provided you have GNU sed installed (other editions of sed usually don't support the -i flag):

GIT_SEQUENCE_EDITOR="sed -i -e '1 {h; d}; $ {p; g}'" git rebase -i <commit>~
0
Ritesh Singh Rajput On

git rebase commit-id

This command do the job for you.