git - Move commits from one branch to another with almost the same file contents but completely different commit history -
ok, have got multi-commit task on branch devel. have move changes branch (production), before month ago, had peculiar system of deployment - after checking on devel, person needed integrate new changes making new commit , integrate manually. don't want that, have 2 branches weren't merged 1,5 years, want move commits, , make new devel branch.
the tree looks that:
ft1 -----------c--------------  dev ---a---b-------d-------e---  prod --f---------------g-------   and want be
ft1 -----------c--------------  dev ---a---b-------d-------e---  prod --f---b-------d---g---e---   how can accomplish it?
inserting b , d commits between f , g not quite normal workflow, no matter vcs 1 uses.
if don't mind applying commits b, d , e on prod branch after g git rebase comes rescue.
git rebase --onto prod dev   as explained in the documentation, command above checks out dev branch, saves commits in current branch (dev) not in a branch (i.e. commits b, d , e) temporary area, checks out prod branch , applies saved commits, 1 one, in original order.
if doesn't fail (because of conflicts) branches this:
ft1 -----------c-------------- dev ---a---b-------d-------e--- prod --f---------------g---b---d---e   if fails have 2 options:
- resolve conflicts, commit run 
git rebase --continue; repeat if needed until commits rebased; - abort rebase running 
git rebase --abort; restore repository in original state. 
how achieve desired status
however, if insist on applying b , d commits before g can use git cherry-pick. 
create new branch (let's name new) starting on commit f , check out. cherry pick commits want, in order want (b, d, g, e):
git checkout -b new f git cherry-pick b git cherry-pick d git cherry-pick g git cherry-pick e   if cherry-picks succeed you're done. hard reset branch prod new remove don't need more (branches dev , new):
git checkout prod git reset --hard new git branch -d new dev   if of cherry picks fail have same 2 options before:
- either resolve conflicts, commit , continue (
git cherry-pick --continue); - or abort everything: checkout branch 
prod, remove branchnew. 
Comments
Post a Comment