I'd like to be able to do this:
git cherry-pick --interactive hash-0..hash-n-1 # fantasy command
and obtain the same workflow as interactive rebase: an editor buffer comes up, containing:
pick hash-0 pick hash-1 pick hash-2 ... pick hash-n-1
where I can delete any unwanted commits, squash
them together, or edit
to pause between the picks to do some manual fixup (like commit --amend
) and all that.
Note how the pick
of interactive rebase is tanalizingly like cherry-pick
.
Now the above operation can be done by performing the cherry-pick first, and then the interactive rebase, which is inconvenient. That is:
$ git tag old-head # mark starting point for later rebase $ git cherry-pick hash-0..hash-n-1 # get everything first $ git rebase --interactive old-head # okay now rebase "in-branch" to fix it up
It's not only inconvenient because of the two steps but because it may require resolving conflicts in commits you don't even want that will be discarded in the rebase stage.
git rebase takes a starting commit and replays your commits as coming after theirs (the starting commit). git cherry-pick takes a set of commits and replays their commits as coming after yours (your HEAD ).
You can run rebase interactively by adding the -i option to git rebase . You must indicate how far back you want to rewrite commits by telling the command which commit to rebase onto. Remember again that this is a rebasing command — every commit in the range HEAD~3..
The git rebase command allows you to easily change a series of commits, modifying the history of your repository. You can reorder, edit, or squash commits together. Typically, you would use git rebase to: Edit previous commit messages. Combine multiple commits into one.
Cherry picking is the act of picking a commit from a branch and applying it to another. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.
Okay, figured out a nice hack.
Start a trivial rebase --interactive HEAD^
over one commit in your current branch. You get something like:
pick 1efd396b * Fixed a bug in frob function
Now, just paste in additional hashes that you want to pick:
pick 1efd396b * Fixed a bug in frob function pick f01934db * Awesome feature added pick 6fd109c1 * Refactored the widgets layer squash 3900fd77 * Refactored the widgets layer s'more
Save and exit, and wee: the rebase
mule obligingly takes the additional cruft you loaded on its back and incorporates it into the current branch according to the commands.
You can actually do an empty rebase with:
git rebase --interactive HEAD
you get a buffer containing
noop
You don't have to delete that; just add your picks after that.
Addendum: To produce the pick lists for this method, use git log --oneline --reverse from..to
, then trim the output needed and prepend the rebase commands to each line: pick
, squash
, ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With