Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: obtain the benefits of `git rebase --interactive` for cherry picks

Tags:

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.

like image 709
Kaz Avatar asked Jun 06 '14 03:06

Kaz


People also ask

What is git rebase and cherry-pick?

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 ).

How do I use git interactive rebase?

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..

What is git rebase pick?

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.

How does cherry picking work in git?

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.


1 Answers

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, ...

like image 123
Kaz Avatar answered Sep 22 '22 05:09

Kaz