Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What tool should I use to split git commit into multiple ones?

I've coded a feature spanning thousand of modified lines in a project, and now it all ended up in one big commit (I've squashed the work-in-progress commits). Now I'd like to split the changes into multiple commits for easier review/possibly multiple PRs.

Some files contain multiple changes that should end up in different commits, so I am looking for a WYSIWIG editor using three-pane view, that would allow me to move some lines to the intermediate commit while seeing the previous and future version (+ some manual changes). I use IntelliJ IDEA for the development, so I imagine something similar to conflict resolution dialog.

I could switch to different branch/tag the single-commit version and do Git->Compare with Branch (and grab the changes from there), but I am looking for something of more convenience.

While this could be marked as off-topic/opinionated, I believe that a neutral suggestion for a different workflow might be valuable.

like image 826
Radim Vansa Avatar asked Mar 31 '17 14:03

Radim Vansa


2 Answers

For IntelliJ I use this workflow:

  • Open the git log window or Alt + 9
  • Right click on commit you want to split → Interactively Rebase from Here
  • Right click on commit you want to split → Stop to edit or Alt + E
  • Start Rebasing
  • Right click to the current commit in the git log with the yellow label 🏷️!Undo commit... alternatively execute git reset HEAD~1 in the terminal
  • Ctrl + K to select and commit your changes with the according message. Repeat as often as necessary until all changes are committed.
  • On the bottom right click: Rebase Stopped for Editing → Continue
like image 67
User Rebo Avatar answered Oct 17 '22 15:10

User Rebo


Not sure about a tool that lets you do this, but, as according to the git documentation you can split a commit using the builtin rebase function. Something like:

git rebase -i
...
pick abc1234 the commit I wanna keep
edit 5904fjl THE COMMIT I WANT TO SPLIT
pick alkj022 my latest commit to keep

After that run git reset HEAD^ and you can manually break out your changes into commits. You can use the git add --patch to more easily split your intra-file changes into commits. Or if your Intellij IDE has a good git GUI you can use that. The splitting, at least in my experience, has to be done from the command line.

like image 30
wspurgin Avatar answered Oct 17 '22 16:10

wspurgin