Without creating a branch and doing a bunch of funky work on a new branch, is it possible to break a single commit into a few different commits after it's been committed to the local repository?
Many Small CommitsThey are easier to understand, easier to test, and easier to review. The complexity of understanding, testing and reviewing a change often increases faster than its size: ten 200-line changes each doing one thing are often far easier to understand than one 2,000 line change doing ten things.
On the command line, navigate to the repository that contains the commit you want to amend. Use the git rebase -i HEAD~n command to display a list of the last n commits in your default text editor. Replace pick with reword before each commit message you want to change.
git rebase -i
will do it.
First, start with a clean working directory: git status
should show no pending modifications, deletions, or additions.
Now, you have to decide which commit(s) you want to split.
To split apart your most recent commit, first:
$ git reset HEAD~
Now commit the pieces individually in the usual way, producing as many commits as you need.
This requires rebasing, that is, rewriting history. To specify the correct commit, you have several choices:
If it is three commits back, then
$ git rebase -i HEAD~3
where 3
is how many commits back it is.
If it is farther back in the tree than you want to count, then
$ git rebase -i 123abcd~
where 123abcd
is the SHA1 of the commit you want to split up.
If you are on a different branch (e.g., a feature branch) that you want to merge into master
:
$ git rebase -i master
When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace pick
with edit
(e
for short). Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:
$ git reset HEAD~
Commit the pieces individually in the usual way, producing as many commits as you need.
Finally
$ git rebase --continue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With