# How Split a Commit in CLI

Aviator CLI helps you splitting a large commit.

## Workflow

1. Check out the commit that you want to split
2. Run `av split-commit`
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.
5. `git add -p` and `git commit` are repeatedly called until there's no diff hunks left.

## Example

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.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2FVux15ykRuW4BDQo2hAI1%2Fsplit_1.png?alt=media&#x26;token=a60106e7-f1bc-4adf-9a1c-dfaf7bae611f" alt=""><figcaption></figcaption></figure>

We run `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 `n` for the changes to `api.go`, and `y` for the changes to `db.go`.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2FKmS3EkFT9FW8qT8fRIXY%2Fsplit_2.png?alt=media&#x26;token=4e0bd6d3-32a4-49f7-8750-3930bfb2fbe3" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Under the hood, this runs `git add --patch`. Please see [<mark style="color:blue;">Interactive Staging</mark>](https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging) for the detailed usage.
{% endhint %}

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.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2F2ioXvkTBxkS0WyT8PDcS%2Fsplit_3.png?alt=media&#x26;token=3e5c720c-df0a-4cb6-a226-5a3bf6b40fbd" alt=""><figcaption></figcaption></figure>

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.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2FmMECncIY7DrDIWeHWlcB%2Fsplit_4.png?alt=media&#x26;token=fba9cf8e-7513-43de-b627-844d3577d6e6" alt=""><figcaption></figcaption></figure>

This time, we want to add all the changes to `api.go`, so respond with `y` for all.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2FzQK0TyyYGyeZOxhxoEKz%2Fsplit_5.png?alt=media&#x26;token=34a3d6eb-5554-4bf6-a250-0fea339df2f2" alt=""><figcaption></figcaption></figure>

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 split-commit` command exits, and you can see that the original commit is split into two.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2FzrsqByaCrnmH98ZoZWZm%2Fsplit_6.png?alt=media&#x26;token=8ce58ae9-df97-4f9c-90b6-8d99802fdfb5" alt=""><figcaption></figcaption></figure>

## Cancelling the command

You might want to stop the splitting process sometimes. Even in that case, your branch is safe. The `av split-commit` command 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 split-commit`. Simply run `git switch --discard-changes $BRANCH_NAME` will get you back to where you were.

<figure><img src="https://273246003-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOAPqUQVbLbsfI5YESl32%2Fuploads%2FHJbyEvGUpuljY7VA3PkH%2Fsplit_fail_1.png?alt=media&#x26;token=4f1c0111-7c0d-41e7-bbf6-e5b67ff5a171" alt=""><figcaption></figcaption></figure>
