Commits
Amend: amending the last commit
5 min read
Amend replaces the branch's last commit with a new one: with the message fixed, with what you staged afterwards, or with both. It's for the classic case of committing and remembering right afterwards a forgotten file, or a typo in the summary.
The amended commit is a new commit (it gets another SHA). If the old commit was already on the remote, the local branch and the remote diverge, and the next push has to be forced.
To amend an older commit on the branch, use Commit surgery ("Amend this commit with the staged changes…") or Edit commit message in the commit menu.
- Commit box › the Amend previous commit checkbox, below the issue reference.
- The commit button then says Amend previous commit.
- If you're going to add changes, stage them first (see Staging).
- Check Amend previous commit.
- If the summary was empty, the box gets the last commit's message (summary and description, with its co-authors). If you had already written something, your text stays.
- Fix the message, if needed.
- Check the button: Amend previous commit (or Amend previous commit without hooks, … and push, depending on the Commit options).
- Click the button or press Ctrl+Enter.
- The last commit is replaced. The box is empty and Amend previous commit is unchecked again.
| Situation | What happens |
|---|---|
| Nothing staged | The amend only replaces the message. The button is enabled even with no staged files |
| Staged files | They go into the amended commit, together with what the commit already had |
| Empty summary when checking | The last commit's message goes into the box |
| Summary already written when checking | The box's text stays as it is and becomes the new message |
| Unchecking after checking | The message that went into the box stays there |
commit.template configured | The template doesn't go into the box while amend is checked |
The checkbox is disabled in a repository that has no commits yet.
git log -1 --format=%B # when checking, with the summary empty: the last commit's message
git commit -F - --amend # with "Skip Git hooks": git commit -F - --amend --no-verify
Git keeps the author and the authoring date of the original commit; the committer and the commit date become the current ones. If signing is on, the amended commit is signed again.
- Ctrl+Z (outside a text field) undoes the amend. The undo button shows "Undo: Commit "Fix the login form validation" (Ctrl+Z)".
- The branch goes back to the original commit, with the original message (
git reset --softto it). What the amend added goes back to Staged files. - Ctrl+Shift+Z redoes the amend.
- If the amend has already been sent with a force push, Ctrl+Z only moves the local branch back; the remote keeps the amended commit.
- Without undo (after closing the app, for example), the original commit is still in the reflog.
- All the checks before the commit also run on amend (team rules, push rules, commitlint, secrets…). See Checks before the commit.
- Protected branch: the "Before committing" dialog warns "You're about to amend the last commit on main, which is a protected branch. Changes to it usually go through their own branch and a merge request." On amend, the dialog doesn't offer to create a new branch. See Checks.
- Amend rewrites the last commit. If other people have already fetched the original commit, talk to them first.
- The same as for the commit box (Preferences › Commit, Operations, Versions and commits).
When the last commit has already been pushed (the branch has an upstream and isn't ahead of it), amend creates a commit the remote doesn't have and removes from the local branch the commit the remote has. A regular push is rejected; a force push is needed.
- Commit box › Amend previous commit, with Push after committing checked in Commit options.
- Or, after the amend, Force push… in the menu of the Push button on the toolbar (see Force push).
- Amend as in the previous section, with Push after committing checked.
- After the commit, the app asks you to confirm the force push, in the Force push dialog: "Overwrite origin/feature/login with your local feature/login? Uses --force-with-lease: fails if the remote has commits you haven't fetched yet."
- Click Force push. The push goes through the push checks first.
- Without Push after committing, use Force push… when you want to send it.
| Option | What it does | Default |
|---|---|---|
| Push after committing | With an amend of a commit already pushed, the push after the commit is forced, with confirmation | Unchecked |
| Don't ask again (in the Force push dialog) | Stops asking for force push confirmation. It comes back in Preferences › Operations › Confirm before force pushing | Unchecked |
git commit -F - --amend
git push --force-with-lease --force-if-includes # the second one only when the git version supports it
The --force-with-lease protection rejects the push if the remote has commits you haven't fetched
yet: someone else's commit isn't erased without you seeing it.
The force push isn't undone with Ctrl+Z. To bring the remote back to the original commit, undo the amend locally (Ctrl+Z) and do another force push.
- If the force push fails because the remote moved, fetch, look at what came in and decide (rebase, merge or a talk with the team) before trying again.
- On a branch protected on the server, the force push is usually rejected; the app warns you beforehand (see Checks).
- "Force push won't ask for confirmation anymore. You can turn it back on in Preferences › Operations.": shows up after you check Don't ask again.
- Preferences › Operations › Force push › Confirm before force pushing.