Splitting a single monster git commit
Sometimes for whatever reason, you are working on a change or a new feature, andone thing leads to another, you find a bug and fix it here, you need some new structure
there, you need to change the return value or the parameters for another existing feature,
you change something, only to find out you didn't need it, etc, etc.
This goes on along as you hack out the details, and finally, the shiny new thing is complete.
You commit it. but when you sit back and look at your new feature, or worse, when your team
gathers around the table for code review, they note that it includes a huge amount of stuff which
isn't part of the feature.
"How are we supposed to even review this? Fix it! Split up the commit."
This post is a guide to how I prefer to split a commit.
- create a new branch to keep the old branch around until we're sure the new one is good
git branch original_monster_commit - If your commit is the most recent one, skip to step 4
- OR if it is not the most recent commit: do an interactive rebase on a commit before the monster, and mark the monster commit with "e" for edit
- git rebase -i some_commit
- do a soft reset to the previous version (This is technically a dangerous thing to do, but remember, we're safe because we created that branch in step 1)
- git reset HEAD~1
- Form a strategy:
- Look at the changes with git diff.
- Ask youself:
- What changes go together?
- What changes are dependent on other changes
- What changes might require temporary changes to stand alone?
- Are there a class of cleanups or whitespace changes that should be bundled
- Repeatedly Add and commit your changesets
- git add -p
- git add <new_filename_if_applicable>
- git commit
- git stash
- make sure your tests pass, compile succeeds, etc
- git stash pop
- finish the rebase (if this was not the most recent commit)
- git rebase --continue
- When there are no uncommitted changes, check it against the saved branch (You did create an extra branch in step 1, didn't you? DIDN'T YOU?!!!!)
- git diff original_monster_commit
- If they are the same, you're done!
No comments:
Post a Comment