fixup
Apply staged changes to a selected commit in history (rewrites history)
Apply the currently staged changes to a commit by rewriting git history.
-
First verify there are staged changes with
git diff --cached --stat -
If no staged changes, give the option to stage all changes. If I chose to stage all the git modified files, run
git add -Aand continue. If not, stop. -
Get the last 8 commits and the staged diff:
git log --oneline -8 git diff --cached --stat -
Determine which staged files and lines are affected:
git diff --cached --name-only git diff --cached --unified=0 -
Rank commits by how well they match the staged changes, preferring in this order:
- Same purpose: commits whose message describes the same feature/fix that the staged changes accomplish
- Same lines: commits that previously touched the same lines in the same files (check with
git log -porgit blame) - Same files: commits that touched any of the same files
Present the ranked list to the user with a recommended commit and a brief reason for the recommendation.
-
Give the user the option to accept the recommendation or select a different commit
-
Once the user selects a commit, create a fixup commit:
git commit --fixup=<selected-hash> -
Perform an interactive rebase with autosquash:
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash <selected-hash>~1 -
Report success or any conflicts that need resolution
If there are rebase conflicts:
- Explain what happened
- Show which files have conflicts
- Tell the user they can resolve conflicts and run
git rebase --continue, or abort withgit rebase --abort