Transforming the history of individual branches into many smaller merged branches

Problem

Basically, I have a bad habit of forgetting about creating branches during development. This leaves me with a massive trace of commits, which is hard to understand where one function starts and then ends.

Possible Solution

The commits are fine, I just want to be able to split a single leading branch into several smaller function branches that merge back into the master.

Current Git Repo:

-- A -- B -- C -- D -- E -- F -- G -- H => master

Perfect Git Repo:

       B -- C => feature1 
     /       \
-- A --------- D -- E --------- H => master
                     \        /    
                       F -- G => feature2

What i tried

I read some other similar questions, but could not find a description of my problem. I tried git rebase, but actually I'm not sure exactly how to use it in this situation, an option may also be required --onto.

, !

+4
3

  • A' commit A, , .
  • branch-name* branch-name - HEAD

-- A -- B -- C -- D -- E -- F -- G -- H => master


       B -- C -- D
     /            \
-- A ------------- M1 -- E ------------ M2 => master
                          \            /    
                            F -- G -- H

, , D H , . , , , , . M1 M2 .

:

git checkout master

git reset --hard A

-- A => master*
    \
      B -- C -- D -- E -- F -- G -- H

git checkout -b feature1 D ( , )

                   => feature1
                 /                       
      B -- C -- D -- E -- F -- G -- H
     /
-- A => master*

git merge --no-ff feature1

                   => feature1
                 /                       
       B -- C -- D -- E -- F -- G -- H
     /            \
-- A ------------- M1 => master*

git cherry-pick E

                     => feature1
                   /                       
       B -- C --  D -- E -- F -- G -- H
     /             \
-- A -------------- M1 -- E' => master*

git checkout -b feature2 H

                     => feature1
                   /                       
       B -- C --  D -- E -- F -- G -- H => feature2*
     /             \
-- A -------------- M1 -- E' => master

git rebase --onto E master

Reset , : E H rebase.


, E, E', --onto - , , , . git rebase --onto F^ master, F^ F Git.

                     => feature1
                   /                       
       B -- C --  D -- E -- F -- G -- H
     /             \
-- A -------------- M1 -- E' => master
                            \
                             F' -- G' -- H' => feature2*

git checkout master

                     => feature1
                   /                       
       B -- C --  D -- E -- F -- G -- H
     /             \
-- A -------------- M1 -- E' => master*
                            \
                             F' -- G' -- H' => feature2

git merge --no-ff feature2

                   => feature1
                 /                       
      B -- C -- D -- E -- F -- G -- H
     /           \
-- A ------------ M1 -- E' --------------- M2 => master*
                          \               /    
                            F' -- G' -- H' => feature2

, - ref, , :

      B -- C -- D => feature1
     /           \
-- A ------------ M1 -- E' --------------- M2 => master*
                          \               /    
                            F' -- G' -- H' => feature2
+4

, , :

git checkout -b master_backup master

git checkout master
git reset --hard [SHA position]

. SHA - , .

git checkout -b feature-name master
git cherry-pick [sha-1]^...[sha-2]

. ^ , [sha-1]

, ,

git checkout master
git merge --no-ff feature

4 ,

+2

The β€œperfect” graph is a bit ambiguous because the merge commands are not clearly indicated. On the chart, Dthey Hseem to be mergers, but they should not be.

Here is one of the possible solutions to restore the history graph:

git checkout master
git reset A --hard
git cherry-pick D
git branch feature1 C
git merge feature1 --no-ff
git cherry-pick E
git branch feature2
git cherry-pick H
git checkout feature2
git cherry-pick F G
git checkout master
git merge feature2 --no-ff

Note that feature1they feature2are still local branches. If necessary, you can press them on the remote. The lines have masterbeen rewritten. You may need to force push it to update it in the remote repository.

0
source

Source: https://habr.com/ru/post/1693855/


All Articles