To split a commit into multiple commits in Git, you can use the git rebase -i command (interactive rebase).
Here’s a step-by-step guide on how to do it:
1. First, identify the commit before the one you want to split. You can use git log to find its hash or the number of commits back.
2. Start an interactive rebase by running:
git rebase -i <commit-hash>
Replace <commit-hash> with the hash of the commit before the one you want to split.
3. In the editor that opens, find the commit you want to split. Change the word pick at the beginning of the line for that commit to edit. Save and close the editor.
4. Git will stop at the commit you want to edit. Now, you can reset the commit while keeping your changes in the index:
git reset HEAD^
5. Now, your changes are unstaged. You can stage portions of the changes using git add -p, which allows you to add chunks of the changes interactively.
6. After staging the desired changes for your first commit, use:
git commit -m "Your first commit message"
7. Repeat the process by staging the next part of the changes and committing again until you have split the original commit into the desired number of commits.
8. Once you’ve made all your commits, continue the rebase process with:
git rebase --continue
This will finalize the rebase with your newly created commits. Remember to be cautious when using rebase, especially if your commits have already been pushed to a shared branch, as this can rewrite history.