Split a Commit
Aviator CLI helps you splitting a large commit.
- 1.Check out the commit that you want to split
av commit split
- 3.Select diff hunks to add to the first commit. This is the same as
git add -p
- 4.A commit editor pops open with the original commit message prefilled.
git add -pand
git commitare repeatedly called until there's no diff hunks left.
In this example, we will split the following commit into two: First commit should have changes to
db.go, and the second commit should have changes to
api.go. Currently, the changes are made in one commit.
av split commit. It prompts you whether you want to add the presented diff hunk to the first commit. In this example, we want to include only changes to
db.go, so we respond with
nfor the changes to
yfor the changes to
After choosing which parts should go to the first commit, it prompts you to enter a commit message. The commit message is pre-populated with the original commit's message that you want to split.
Once you update the commit message, close the editor.
Since there are changes that aren't committed to the first commit, we will continue with choosing what to include in the second commit. It prompts you which diff hunks to be in the second commit.
This time, we want to add all the changes to
api.go, so respond with
It pops open an editor for the commit message of the second commit. Update, save, and close the editor.
At this point, we split all diff hunks to commits. The
av commit splitcommand exits, and you can see that the original commit is split into two.
You might want to stop the splitting process sometimes. Even in that case, your branch is safe. The
av commit splitcommand will not touch the branch until the entire process finishes.
When a command is cancelled in the middle, it shows a command on how to get back to the original state when you start
av commit split. Simply run
git switch --discard-changes $BRANCH_NAMEwill get you back to where you were.