linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Everyday workflow/amend

kb/workflow ── Everyday workflow ── beginner

git commit --amend

Rewrites the last commit: changes the message, folds in forgotten edits, or updates the content. Creates a new object with a new SHA. Safe before push; dangerous after.

view as markdownaka: commit-amend, git-amend

git commit --amend does not create a new commit. It replaces the last one. Precisely: it creates a new commit with updated content and moves the branch to it. The old commit remains in .git/objects/ as a dangling object.

What you can do

Fix the message

bash
git commit --amend -m "new message"
# Without -m, the editor opens with the current message.
git commit --amend

Add a forgotten file

bash
git add forgot.js
git commit --amend --no-edit
# --no-edit keeps the old message; the file is folded into the commit.

Fix the content

If you committed and immediately spotted a bug, fix the file, stage it, then --amend. You get a correct commit instead of a separate "fix previous commit" entry.

What physically happens

  1. The current index is taken (including new edits, if any).
  2. A new commit object is created:
    • tree: from the current index
    • parent: the same as the old commit's parent (not the old commit itself)
    • author/committer/message: from the old commit, plus your changes
  3. The current branch moves to the new SHA.
  4. The old commit becomes dangling.

The old commit is visible in reflog for about a month. If you made a mistake in the amend, you can recover it with git reflog.

When not to use it

If the commit was already pushed and someone else pulled it, --amend breaks history. You have one SHA locally; others have a different one. A regular push will fail:

! [rejected]        feature -> feature (non-fast-forward)

Options:

  • Force-push with --force-with-lease. Safe only for a feature branch that nobody else is watching.
  • Do not amend. Create a new commit instead. This is the right approach for main/master and any shared branch.

Rule: amend is allowed before the first push. After push, only on personal branches, only with force-with-lease.

Pitfalls

  • Author date. --amend keeps the author date unchanged but updates the committer date. git log --pretty=fuller shows both.
  • GPG signatures. If the commit was signed, the signature is dropped after --amend. Re-sign with git commit --amend -S.
  • Ordering. git log sorts by committer date by default. After an amend the commit floats to the top, even if the author date is old.

§ команды

bash
git commit --amend -m "new text"

Change the message of the last commit

bash
git commit --amend --no-edit

Fold the index into the last commit without changing the message

bash
git push --force-with-lease

Push after amend; safer than --force

bash
git commit --amend --date="now"

Also update the author date to the current time

§ см. также

  • commit-cmdgit commitCaptures what is in the index as a new commit object and moves the current branch to that commit. Without arguments it opens an editor for the message; the most common form uses `-m`.
  • interactive-rebaseInteractive rebase`git rebase -i <base>` opens an editor with the list of commits from `<base>` to HEAD, where you can rename, combine, delete, and reorder them. It is the primary tool for cleaning up local history.
  • refloggit reflogA log of all HEAD movements on this machine. By default, entries for reachable commits live for 90 days; entries for unreachable commits live for 30. The primary recovery tool after `reset --hard`, `--amend`, `rebase`, and branch deletion. Stored locally in `.git/logs/`.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies