Git: I have a feature branch with 80+ commits. How can I safely bundle it into development / production without breaking the story?

I've been working on a feature subdivision for several weeks and have a history of about 80 commits related to work in many areas of our project. For small features, I would just rebase, squash the commits into one compressed commit, merge it to evolve, and push it. But here I have amassed a long history with commits covering many different parts.

I would like to split them into smaller commits, i.e. "Finished part A of function F" "Finished part B", etc., but the work was not linear and each part was created at the same time.

I could crush them all into one commit and push it, but I will lose all history and that could make future debugging a pain because it's such a big change.

I'm new to using git in a professional environment, so I'm not sure about the best practices. What would be your approach to this situation?

+3


source to share


1 answer


Common aproach

  • Mixed function split commits
  • Change order of commitments

I am assuming your repository looks like this

o--------o----------o----------o
A        B          C          D

      

and that changes to your function are ambiguous and distributed

- feature 1 changes are in A,B,C
- feature 2 changes are in A,C,D
- feature 3 changes are in B,C,D

      

Split commits

If you want to make changes for each function on a line, you must split the commits first.

$ git rebase -i HEAD~3

      

which will open the editor

pick A ...
pick B ...
pick C ...
pick D ...

      

Since you need to edit every commit change, it is

edit A ...
edit B ...
edit C ...
edit D ...

      



Save and exit the editor and git will start reinstalling. git will redirect A and pause so you can edit. You can now reset commit A and make new commits to reflect every change in the function.

$ git reset HEAD~
$ git add ... # only files of feature A
$ git commit -m 'Feature A'
$ git add ... # only files of feature B
$ git commit -m 'Feature B'
$ git add ... # only files of feature C
$ git commit -m 'Feature B'
$ git rebase --continue

      

Git continues and you have to repeat the above example over and over until all commits are split. Your repository which looks like

o---o---o---o---o---o---o---o---o
E   F   G   H   I   J   K   L   M

      

See also Break a previous commit by multiple commits

Change order of commits

Now you have all the changes in one function in one commit. But they are not in linear order:

  • function 1 changes to E, G, I
  • function 2 changes are in F, J, L
  • function 3 changes in H, K, M

Now you can do another one rebase -i

and change the commit order.

pick E
pick G
pick I
pick F
pick J
pick L
pick H
pick K
pick M

      

Save and exit the interactive redirect editor and git will change the commit order. You might get merge conflicts that you should resolve. The commits will be reordered and therefore may conflict with the preemptive commit now.

Hopefully git rebase finishes without any conflicts and you are taken to this repository

 o---o---o---o---o---o---o---o---o
 N   O   P   Q   R   S   T   U   V

|         | |         | |         |
+---------+ +---------+ +---------+
 feature 1   feature 2   feature 3

      

+5


source







All Articles