Amending and Rewriting Commits
git commit --amend replaces the most recent commit with a new one that combines the previous commit's changes with whatever is currently staged, and optionally a new message. It is the fastest way to fix a typo in a commit message, add a forgotten file, or tweak a line you just committed, without cluttering history with a separate 'fix' commit. Because amend creates a brand-new commit object with a new hash, it is a form of history rewriting — a smaller-scoped cousin of interactive rebase — and the same sharing rules apply: amend freely on commits nobody else has fetched, but avoid it on commits that have already been pushed and pulled by others.
Cricket analogy: Think of a scorer at Eden Gardens correcting Virat Kohli's boundary tally before submitting the innings card to officials — easy to fix privately, but once the card is announced to the crowd (pushed), changing it causes chaos.
Amending the Message vs. the Content
Running git commit --amend with nothing staged opens your editor on the previous commit's message, letting you reword it while keeping the same file changes. If you stage additional changes first (git add some file), amend folds those changes into the previous commit as well, producing one commit that looks as if everything had been committed together the first time. git commit --amend --no-edit keeps the existing message unchanged while still folding in newly staged changes — handy for quickly absorbing a forgotten file or a one-line review fix.
Cricket analogy: Reopening the scorecard to just fix Rohit Sharma's misspelled name keeps the same runs total, but adding a missed boundary before resubmitting folds both into the entry; --no-edit fixes the run tally silently, leaving the name untouched.
Beyond the Last Commit
Amend only touches the tip commit. To reword, reorder, split, or edit commits further back in history, you need interactive rebase (git rebase -i), which offers reword and edit actions for exactly that purpose. For bulk, scripted rewrites across many commits or many files — such as removing a secret that was committed dozens of commits ago, or changing an author's email across the whole repository — dedicated history-rewriting tools like git filter-repo (the modern, recommended replacement for the older, slower git filter-branch) operate on the entire history in one pass rather than commit-by-commit.
Cricket analogy: Amend can only fix the very last ball bowled this over; correcting a wide called three overs ago needs the umpires to review the whole innings (interactive rebase), and purging every no-ball across a tournament's records needs a full audit (filter-repo).
# Fix a typo in the last commit's message
git commit --amend -m "Fix null pointer in checkout flow"
# Forgot to add a file to the last commit
git add src/utils/currency.js
git commit --amend --no-edit
# Amend without opening an editor, keeping the message
git add -A
git commit --amend --no-edit
# If the amended commit was already pushed to your own feature branch:
git push --force-with-lease origin feature/checkout-fix
# Rewrite an older commit's message (not just the last one) via interactive rebase
git rebase -i HEAD~4 # mark the target commit as 'reword'--force-with-lease is safer than plain --force after amending a pushed branch: it refuses to overwrite the remote branch if someone else has pushed new commits to it since you last fetched, preventing you from silently discarding a teammate's work. Get in the habit of using --force-with-lease by default, and reserve plain --force for cases where you deliberately want to override without that safety check.
Amend is really just shorthand for 'reset the last commit, then make a new one' — under the hood Git creates a new commit object pointing at the same parent as the old one, and moves the branch pointer to it. The old commit object still technically exists (findable via git reflog) until garbage collected, which is why an accidental bad amend is usually recoverable.
git commit --amendreplaces the last commit with a new one, combining currently staged changes with the previous commit's content.--amend --no-editfolds in new changes while keeping the existing commit message unchanged.- Amend only affects the tip commit; use interactive rebase's
reword/editto change commits further back. - Amending creates a new commit hash, so it counts as history rewriting — avoid it on already-shared commits.
git push --force-with-leasesafely updates a remote branch after amending, refusing to clobber others' unseen pushes.- Dedicated tools like
git filter-repohandle bulk rewrites (e.g. removing a secret) across the entire history at once.
Practice what you learned
1. What does `git commit --amend --no-edit` do?
2. Which command can reword or edit a commit that is NOT the most recent one?
3. Why is `git push --force-with-lease` generally preferred over `git push --force` after amending a pushed commit?
4. What happens to the original commit object after you run `git commit --amend`?
5. Which tool is recommended for bulk-rewriting history across many commits, such as purging a committed secret repository-wide?
Was this page helpful?
You May Also Like
Interactive Rebase
Interactive rebase lets you reorder, squash, edit, or drop commits before they land, turning a messy work-in-progress history into a clean, reviewable narrative.
git reset Explained
How git reset moves the current branch pointer and optionally rewrites the staging area and working directory, and the meaningful difference between its --soft, --mixed, and --hard modes.
Commit Message Conventions
Why disciplined, structured commit messages make history searchable and automatable, and how conventions like Conventional Commits standardize their format.
Recovering Lost Commits with reflog
Discover how git's reflog silently tracks every movement of HEAD and branch tips, giving you a safety net to recover commits after resets, rebases, or accidental deletions.
Related Reading
Related Study Notes in Software Engineering
Browse all study notesMicroservices Study Notes
Software Architecture · 30 topics
Software EngineeringTesting & TDD Study Notes
Software Testing · 30 topics
Software EngineeringDesign Patterns Study Notes
Software Design · 30 topics
Software EngineeringSoftware Engineering Study Notes
Python · 40 topics
Software EngineeringSystem Design Study Notes
Architecture · 40 topics