git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: User's mailing list? And multiple cherry pick

From
Johan Herland <johan@herland.net>
Date
Jun 4, 2008, 08:16 UTC
Message-ID
<200806041016.26066.johan@herland.net>
In-Reply-To
<18c1e6480806040013l72da09aem30f91183e4fcbe41@mail.gmail.com>
On Wednesday 04 June 2008, David wrote:
Show 9 quoted lines
> > rebase --onto?
> 
> Thanks, I checked the manuals further, and it looks like this will
> (mostly) do what I need.
> 
> What's still missing is multiple cherry pick ;-)
> 
> In other words, is there a simple way to *copy* a large number of
> commits from one branch to another, without rebasing?
Depends on you definition of "simple". Here are 5 commands that gets the job done in a (IMHO) conceptually simple way.
Use
	git checkout -b [tmpBranch] [fromBranch]
to create (and checkout) a _new_ branch pointing to the same commit as the "from"-branch. Then use
	git rebase --onto [toBranch] [fromBranchStart] [tmpBranch]
to rebase all commits between [fromBranchStart] and [fromBranch] on top of [toBranch]. Since this was done with HEAD == [tmpBranch], the original [fromBranch] is not moved, and therefore *still* points to the old commits. [tmpBranch], however, have been moved to point at the rebased commits. This in effect *copies* the commits from [fromBranch] to [tmpBranch]. Now, all that remains is to reconcile [tmpBranch] and [toBranch], and finally remove [tmpBranch]:
	git checkout [toBranch]
	git merge [tmpBranch]
	git branch -d [tmpBranch]
The merge should be a simple fast-forward without any conflicts.
Here is an illustrated version of what's going on:
Initial layout:
	          G---H---I  <---- [fromBranch]
	         /
	A---B---C---D---E---F  <-- [toBranch]
	        ^----------------- [fromBranchStart]
git checkout -b [tmpBranch] [fromBranch]
	          G---H---I  <---- [fromBranch]
	         /        ^------- [tmpBranch]      
	A---B---C---D---E---F  <-- [toBranch]
	        ^----------------- [fromBranchStart]
git rebase --onto [toBranch] [fromBranchStart] [tmpBranch]
	          G---H---I  <------------------- [fromBranch]
	         /              
	A---B---C---D---E---F---G'---H'---I'  <-- [tmpBranch]
	        ^           ^-------------------- [toBranch]
	        --------------------------------- [fromBranchStart]

git checkout [toBranch] git merge [tmpBranch] git branch -d [tmpBranch]

	          G---H---I  <------------------- [fromBranch]
	         /              
	A---B---C---D---E---F---G'---H'---I'  <-- [toBranch]
	        ^-------------------------------- [fromBranchStart]
This should also work even if your commit graphs are considerably more complicated.
If you need to drop/edit/squash any commits between [fromBranchStart] and [fromBranch], simply add a "--interactive" to the rebase command.
Have fun!
...Johan
-- 
Johan Herland, <johan@herland.net>
www.herland.net
Previous: Björn SteinbrinkNext: Jakub Narebski
Message 12 of 22 in “User's mailing list? And multiple cherry pick”
  1. DavidJun 4, 2008
  2. Junio C HamanoJun 4, 2008
  3. DavidJun 4, 2008
  4. Jakub NarebskiJun 4, 2008
  5. Johan HerlandJun 4, 2008
  6. DavidJun 4, 2008
  7. Wincent ColaiutaJun 4, 2008
  8. DavidJun 4, 2008
  9. Wincent ColaiutaJun 4, 2008
  10. Jakub NarebskiJun 4, 2008
  11. Björn SteinbrinkJun 4, 2008
  12. Johan HerlandJun 4, 2008
  13. Jakub NarebskiJun 4, 2008
  14. DavidJun 4, 2008
  15. Jakub NarebskiJun 4, 2008
  16. DavidJun 4, 2008
  17. Jakub NarebskiJun 4, 2008
  18. Theodore TsoJun 4, 2008
  19. Karl HasselströmJun 4, 2008
  20. Miklos VajnaJun 4, 2008
  21. Stephan BeyerJun 4, 2008
  22. DavidJun 4, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.