Skip to content
Waleed Khan edited this page Feb 11, 2023 · 73 revisions

Description

New in v0.3.8.

git amend is used to amend the current commit and restack any descendant commits. It's a replacement for git commit --amend.

Unlike git commit --amend:

  • git amend never updates the commit message, only the commit contents.
  • git amend automatically tries to restack any descendant commits. This is useful if you're modifying a commit which is earlier in a stack.

Usage

Make some changes:

$ echo 'Hello, world!' >>README.md 
$ git status
On branch bug-report
Your branch and 'origin/bug-report' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

(You don't need to stage any changes.)

Then, to amend the current commit, run:

$ git amend
branchless: running command: git reset
Attempting rebase in-memory...
[1/1] Committed as: 89f87c23 temp: work on moving individual commits
branchless: processing 1 update: branch move-individual-commits
branchless: processing 1 rewritten commit
branchless: running command: git checkout bug-report
Switched to branch 'bug-report'
Your branch and 'origin/bug-report' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
branchless: processing checkout
In-memory rebase succeeded.
Finished restacking commits.
No abandoned branches to restack.
⋮
◇ 97f5fc72 2h (remote origin/master) docs: update CHANGELOG.md
┃
◯ 6372ca8e 54m feat(bug_report): create `bug-report` command
┃
● 933d2489 0s (ᐅ bug-report) feat(bug_report): use `bugreport` library to collect extra information
┃
◯ 89f87c23 0s (move-individual-commits) temp: work on moving individual commits
Amended with 1 uncommitted change.

Note that git amend attempted to restack all descendant commits (and succeeded).

Amending only some changes

If you want to amend the current commit with only some changes, you can stage them first:

$ git add my-file
$ git amend  # only updates with `my-file`

By doing this, git amend knows to use the staged changes for the amend, and leaves the unstaged changes in place.

Clone this wiki locally