Git provides the git commit --amend command to modify the most recent commit without creating a new commit in the branch history. To understand commit structure, see
git commit. It allows you to update the commit message, add forgotten files, change the author, or adjust commit metadata as part of your daily
git workflow.
In this guide, we will explore how to use git commit --amend through practical scenarios, common use cases, and quick reference commands that developers frequently use during daily Git workflows.
Git Commit Amend - Quick Cheat Sheet
The following tables summarize the most common git commit --amend workflows developers use in daily development.
Edit the Last Commit Message
| Step | Command |
|---|---|
| Amend commit message | git commit --amend -m "updated commit message" |
| Verify updated commit using git log | git log --oneline |
Add Forgotten Files to the Last Commit
| Step | Command |
|---|---|
| Stage missing file using git add | git add config.yaml |
| Amend previous commit | git commit --amend --no-edit |
| Verify commit changes | git log --oneline |
Amend Commit Without Changing Message
| Step | Command |
|---|---|
| Stage new changes | git add . |
| Update previous commit | git commit --amend --no-edit |
| Check commit history | git log --oneline |
Change Author of the Last Commit
| Step | Command |
|---|---|
| Update commit author | git commit --amend --author="John Doe <john@example.com>" |
| Verify author change | git log |
Update Commit Timestamp
| Step | Command |
|---|---|
| Update commit date | git commit --amend --no-edit --date=now |
| Verify commit timestamp | git log --pretty=fuller |
Fix a Pushed Commit
| Step | Command |
|---|---|
| Stage missing changes | git add . |
| Amend commit | git commit --amend --no-edit |
| Push rewritten commit using git push | git push --force-with-lease |
Undo git commit --amend
| Step | Command |
|---|---|
| View commit history changes using git reflog | git reflog |
| Restore previous commit using git reset | git reset --soft HEAD@{1} |
| Restore commit completely | git reset --hard HEAD@{1} |
Modify Older Commits (Alternative to Amend using git rebase)
| Step | Command |
|---|---|
| Start interactive rebase | git rebase -i HEAD~3 |
| Edit selected commit | git commit --amend |
| Continue rebase | git rebase --continue |
General git commit --amend Reference
| Command | Description |
|---|---|
git commit --amend | Modify the most recent commit and open editor to edit the message |
git commit --amend -m "message" | Replace the last commit message directly from terminal |
git commit --amend --no-edit | Amend commit without modifying the message |
git commit --amend --author="Name <email>" | Update author information |
git commit --amend --date=now | Change commit timestamp |
git push --force-with-lease | Push amended commit safely after history rewrite |
git reflog | View commit history changes including amend operations |
Scenario 1: Edit the Last Commit Message
Sometimes you may realize that the last commit message contains a typo, incorrect description, or missing information. Instead of creating a new commit, you can modify the message of the most recent commit using the git commit --amend command.
Change commit message using git commit --amend -m
If you want to quickly update the last commit message from the command line, you can use the -m option.
git commit --amend -m "Updated commit message"This replaces the previous commit message with the new one without creating a separate commit.
Edit commit message using Git editor
If you run the amend command without the -m option, Git opens the default editor so you can manually update the commit message, similar to how commits are created using
git commit command.
git commit --amendAfter editing the message, save and close the editor to apply the changes.
Verify updated message using git log
After amending the commit, you can confirm the change by checking the commit history.
git log --onelineThis displays the latest commits along with the updated commit message.
Scenario 2: Add Forgotten Files to the Last Commit
During development, it is common to accidentally forget adding a file before committing changes. Instead of creating another commit, you can add the missing files to the previous commit using the amend command.
Stage new files before amending
First, add the missing files to the staging area.
git add <file>For example:
git add config.yamlAdd files to previous commit using git amend
Once the file is staged, amend the previous commit to include the new changes.
git commit --amendGit will open the commit editor where you can modify the message if needed.
Keep existing message using --no-edit
If you want to add the new files while keeping the same commit message, use the --no-edit option.
git commit --amend --no-editThis updates the previous commit with the staged changes without opening the editor.
Scenario 3: Amend Commit Without Changing Message
In many cases, developers want to modify the last commit by adding files or making small fixes but keep the same commit message.
Use git commit --amend --no-edit
The --no-edit option allows you to amend the previous commit without modifying the commit message.
git commit --amend --no-editThis command updates the last commit with the currently staged changes.
When developers use no-edit option
The --no-edit option is useful in situations such as:
- Adding a forgotten file to the last commit
- Fixing a minor typo in code after committing
- Updating configuration files related to the last commit
It helps keep the commit history clean by avoiding unnecessary extra commits.
Scenario 4: Change Author of the Last Commit
Sometimes the author information of a commit may be incorrect due to misconfigured Git settings. The amend command allows you to update the author details of the most recent commit.
Update commit author name
You can update the author name and email using the --author option.
git commit --amend --author="John Doe <john@example.com>"This replaces the author information for the last commit.
Update commit author email
If only the email address needs to be updated, provide the corrected email inside the author parameter.
git commit --amend --author="John Doe <john.doe@company.com>"Verify author change using git log
After updating the author information, verify the changes using the log command.
git logThe output will show the updated author details for the amended commit.
Scenario 5: Change Commit Timestamp
In some cases you may want to update the timestamp of the last commit, for example when correcting metadata or recreating a commit with a new time reference. Git allows you to modify the commit date while amending the latest commit.
Reset commit timestamp to current time
To update the commit timestamp to the current system time, use the amend command with the --no-edit option.
git commit --amend --no-edit --date=nowThis keeps the existing commit message but updates the commit date.
Set custom commit date using --date
You can also manually specify a commit date.
git commit --amend --no-edit --date="2026-03-16 10:30:00"This replaces the previous timestamp with the custom date.
Verify timestamp change
After amending the commit, verify the updated timestamp.
git log --pretty=fullerThis command displays detailed commit information including author date and commit date.
Scenario 6: Fix a Pushed Commit Using Amend
Sometimes you may realize that the last commit contains mistakes even after pushing it to a remote repository. While it is possible to amend the commit, doing so rewrites history and requires caution.
Why amending pushed commits is dangerous
When you amend a commit that has already been pushed, the commit hash changes. This causes history divergence for other collaborators who already pulled the original commit.
Safely update pushed commit using force-with-lease
After amending a pushed commit, you must force push the updated commit to the remote repository.
git push --force-with-leaseThe --force-with-lease option is safer than a regular force push because it ensures you do not overwrite changes from other contributors.
Example workflow for amending pushed commits
A typical workflow may look like this:
git add config.yaml
git commit --amend --no-edit
git push --force-with-leaseThis updates the previous commit and safely pushes the rewritten history.
Scenario 7: Undo git commit --amend
If you accidentally amend a commit or want to revert the changes, Git provides tools to recover the previous commit state.
Recover previous commit using git reflog
The reflog command shows a history of changes to HEAD, including amend operations.
git reflogLocate the commit reference before the amend operation.
Restore commit using git reset
Once you find the previous commit reference, you can reset the repository to that state.
git reset --soft HEAD@{1}This restores the previous commit while keeping the changes staged.
Recover commit history safely
If you want to completely revert the amend operation including staged changes, use:
git reset --hard HEAD@{1}This returns the repository to the exact state before the amend command.
Scenario 8: Amend Older Commits (Not the Last One)
The git commit --amend command only modifies the most recent commit. If you need to edit older commits, you must use Git’s interactive rebase feature.
Why git commit amend only works for last commit
Git only allows the amend operation on the latest commit because changing older commits would require rewriting the entire commit history that follows.
Modify older commits using git rebase interactive
To edit an earlier commit, start an interactive rebase.
git rebase -i HEAD~3This opens a list of the last three commits in an editor. Change pick to edit for the commit you want to modify.
After Git pauses at that commit, run:
git commit --amend
git rebase --continueThis allows you to modify older commits while preserving the remaining commit history.
Practical git commit --amend Examples
The git commit --amend command is commonly used in day-to-day development to correct small mistakes or update the most recent commit without creating a new commit. Below are some practical scenarios where developers frequently use this command.
Fix typo in commit message
If you accidentally write an incorrect commit message, you can quickly fix it using the amend command.
git commit --amend -m "Fix typo in login validation"This replaces the previous commit message with the updated one while keeping the commit content unchanged.
Add configuration file missed during commit
Sometimes developers forget to include a configuration file or script while committing changes. Instead of creating another commit, you can add the file to the previous commit.
git add config.yaml
git commit --amend --no-editThis updates the previous commit by including the new file while keeping the original commit message.
Combine small fixes into one commit
During development, you might make several small fixes immediately after committing. Instead of creating multiple commits, you can include those changes in the previous commit.
git add .
git commit --amend --no-editThis approach helps maintain a cleaner commit history by keeping related changes in a single commit.
Correct author details
If the Git username or email was configured incorrectly when making a commit, you can update the author information using the amend command.
git commit --amend --author="Jane Doe <jane.doe@example.com>"This modifies the author information for the last commit without changing the actual code changes.
Frequently Asked Questions
1. What does git commit --amend do?
Thegit commit --amend command modifies the most recent commit in a Git repository. It allows you to change the commit message, add forgotten files, update author information, or modify commit metadata without creating a new commit in the branch history.2. Can I amend a commit after pushing it to remote?
Yes, but it rewrites commit history. After amending a pushed commit, you must push the updated commit usinggit push --force-with-lease. This should be done carefully because it can affect collaborators working on the same branch.3. How do I undo git commit --amend?
You can undo an amend operation usinggit reflog to locate the previous commit and then restore it with git reset --soft HEAD@{1} or git reset --hard HEAD@{1} depending on whether you want to keep changes.4. Can git commit --amend modify older commits?
No. Thegit commit --amend command only modifies the latest commit. To edit older commits, you must use interactive rebase with git rebase -i.5. Does git commit --amend create a new commit?
Yes. Even though it modifies the previous commit, Git actually creates a new commit with a new commit hash and replaces the old commit in the history.Summary
The git commit --amend command allows developers to modify the most recent commit in a Git repository. For advanced history operations, see
git rebase. It can be used to correct commit messages, add forgotten files, update author information, or adjust commit metadata. When used properly, it helps maintain a clean and meaningful commit history during development.
However, developers should avoid amending commits that have already been pushed to shared repositories unless they fully understand the implications of rewriting commit history.
Official Documentation
For more details about the amend command and commit options, refer to the official Git documentation:



![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)






