From f2dce683e57801d5adf94dfbaac9bd1fcffc88f6 Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Sat, 10 Feb 2024 19:54:36 +0100 Subject: [PATCH] docs: Add instructions for git commit --fixup to change older commits. Zulip PRs often have multiple commits and review process requires contributors to make changes to the code in other commits than just the latest one, which requires more familiarity with git rebase than most other tasks. `git commit --fixup` with `git rebase -i --autosquash` is probably the most direct and efficient way specifically for this task, so it seems useful to have a recommendation about it. --- docs/git/fixing-commits.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/git/fixing-commits.md b/docs/git/fixing-commits.md index 018d9ae34e..cfa4463435 100644 --- a/docs/git/fixing-commits.md +++ b/docs/git/fixing-commits.md @@ -40,6 +40,30 @@ Sometimes, you want to make one commit out of a bunch of commits. To do this, 1. `git rebase -i HEAD~n` where `n` is the number of commits you are interested in 2. Reorder the lines containing the commits and save +## Changing code in an older commit + +If your pull request has multiple commits, a reviewer may request changes that require +you to make changes to an older commit than the latest one. This tends to be more tedious +than just amending the last commit and there can be various approaches, but the most efficient +is by using `git commit --fixup`: + +1. Make your changes to the files and stage them, as usual. +1. Identify the hash of the commit you want to modify - for example by copying it from + `git log --oneline`. +1. Commit your changes as a fixup, by using `git commit --fixup `. +1. A new commit with its message starting with `fixup!` has been created. Now you can + run `git rebase -i --autosquash HEAD~n`, replacing `n` with the number of commits + to include in the rebase, as in the other sections. In the interactive + view the commits will be automatically ordered appropriately so that the fixups get + squashed into the corresponding commits. +1. (Optional) If you want to avoid having to include `--autosquash` every time, you + can add the following configuration to your `~/.gitconfig`: + + ``` + [rebase] + autosquash = true + ``` + ## Pushing commits after tidying them 1. `git push origin +my-feature-branch` (Note the `+` there and substitute your actual branch name.)