How to split a git commit

Greg Foster
Greg Foster
Graphite software engineer

Splitting a commit allows developers to break down large sets of changes into smaller, more focused snapshots. This practice can improve code review efficiency and maintain a clear project history. Here's how to split a commit using Git and Graphite CLI.

Ensure you have a commit that needs splitting and its parent's commit hash.

To split a single commit into multiple smaller commits in Git, you will typically use an interactive rebase:

Terminal
# Start an interactive rebase for the last 3 commits.
# Adjust the number as necessary for your specific case.
git rebase -i HEAD~3

In the interactive rebase file that opens:

  1. Replace pick with edit next to the commit you want to split.

  2. Save and close the file to start the rebase process.

Once the rebase has started and you are on the commit you want to split:

Terminal
# Reset the commit, but leave the changes in the working directory.
git reset HEAD~

Now stage and commit parts of the changes iteratively:

Terminal
# Add files or parts of files you want to include in the first new commit.
git add <file1> <file2> ...
# or interactively choose parts of files to stage
git add -p
# Commit the staged changes.
git commit -m "First part of the split commit"

Repeat the git add and git commit steps until all changes are committed separately.

Finally, continue the rebase:

Terminal
# Continue the rebase process after all changes are committed.
git rebase --continue

Repeat the process of editing, adding, and committing if you have more commits to split. Once done, verify the new commit history with git log.

This process allows you to break up a commit into smaller, more focused commits that can be reviewed and understood more easily.

Graphite CLI offers two primary methods for splitting commits: by commit and by hunk. This allows for precise control over how you want to divide the changes in your branch.

  1. Identify the Commit:
    Use gt log to identify the commit hash that you want to split.

  2. Execute the Split:
    Run gt split --by-commit. The CLI will prompt you to select commits to split into separate branches.

  3. Branch Creation:
    Graphite automatically creates new branches at each commit you selected, turning them into single-commit branches.

  1. Select the Branch:
    Choose the current branch you wish to split by hunk.

  2. Run the Split Command:
    Execute gt split --by-hunk to enter the interactive mode, which utilizes the git add --patch interface.

  3. Hunk Selection:
    Graphite will present you with hunks of changes. Select the hunks that you want to include in the first new commit.

  4. Commit Creation:
    After hunk selection, Graphite will guide you through creating a new single-commit branch for the chosen changes.

  5. Repeat Process:
    Continue selecting hunks and creating new commits as needed.

After splitting the commits:

  1. Stack Management:
    Use gt stack to review your stack and ensure that the commits have been split correctly and the new branches are properly ordered.

  2. Integration with Main Branch:
    Once satisfied, integrate the new branches into the main branch as needed, ensuring that the stack is rebased properly.

  • Simplicity: Graphite CLI provides a more straightforward interface for splitting commits, especially when dealing with hunks.

  • Efficiency: The process is faster as it reduces the steps and commands required compared to traditional Git.

  • Automation: Branch creation and commit partitioning are automated, minimizing the potential for errors.

  • Interactive Mode: The interactive mode allows for selective commit splitting, giving you fine-grained control over the process.

Splitting commits can enhance your repository's clarity. Git offers granular control with its interactive rebase, while Graphite CLI streamlines the process, making it accessible even for those less familiar with Git's intricacies. Both methods have their place in a developer's toolkit.

For detailed command usage and further reading, refer to the official documentation at Graphite.dev/docs and consider adding links to specific sections such as the Graphite CLI commands for the gt split-commit reference.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2