Break a Previous Commit into Multiple Commits

Better Stack Team
Updated on July 25, 2024

To break a previous commit into multiple commits in Git, you can use an interactive rebase. This process allows you to edit, split, or reorder commits starting from a chosen commit in your Git history. Here’s a step-by-step guide on how to achieve this:

Step-by-Step Guide to Break a Previous Commit into Multiple Commits

1. Identify the Commit to Edit

First, identify the commit from which you want to start splitting into multiple commits. You will need the hash or a reference to this commit.

2. Start an Interactive Rebase

Open your terminal or command prompt, navigate to your Git repository, and start an interactive rebase session. Specify the commit just before the one you want to split (<commit> can be the commit hash or a reference):

 
git rebase -i <commit>~1

For example, if you want to edit the 5th commit back in history, you would use:

 
git rebase -i HEAD~5

3. Interactively Rebase

Git will open a text editor with a list of commits starting from the specified commit. Each commit will start with the word pick. Your editor might look like this:

 
pick abc1234 Commit message 1
pick def5678 Commit message 2
pick 1234567 Commit message 3

4. Edit Commits

To break a commit into multiple commits:

  • Change pick to edit (or e) for the commit you want to split.
  • Save and close the editor.

5. Split the Commit

Git will stop at the chosen commit for you to make changes:

  1. Reset the Commit:

     
    git reset HEAD^
    

    This resets the HEAD to the previous commit (the commit before the one you're splitting).

  2. Stage Changes:

    Stage the changes you want to include in the first new commit.

     
    git add file1 file2 ...
    
  3. Commit the Changes:

    Commit the staged changes.

     
    git commit -m "First part of split commit"
    
  4. Stage and Commit Remaining Changes:

    Stage and commit the remaining changes as needed.

     
    git add file3 file4 ...
    git commit -m "Second part of split commit"
    

6. Continue Rebase

Once you've split the commit into multiple commits:

  • Continue the rebase to apply the remaining commits.
 
git rebase --continue

7. Resolve Conflicts (if any)

During the rebase, Git might pause for you to resolve conflicts. Follow the prompts in your terminal to resolve any conflicts that arise.

8. Finish the Rebase

Once you've resolved conflicts and completed splitting the commits:

  • If everything is successful, Git will finish the rebase automatically.

Summary

Using interactive rebase (git rebase -i) allows you to edit and split commits in your Git history effectively. This method is powerful but requires caution, especially if you are rewriting history that others may have already based work on. Always communicate with your team if you're making significant changes to shared branches or history.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github