Git Commit --Amend Explained (Edit Last Commit Message, Files & Author)

Git Commit --Amend Explained (Edit Last Commit Message, Files & Author)

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

StepCommand
Amend commit messagegit commit --amend -m "updated commit message"
Verify updated commit using git loggit log --oneline

Add Forgotten Files to the Last Commit

StepCommand
Stage missing file using git addgit add config.yaml
Amend previous commitgit commit --amend --no-edit
Verify commit changesgit log --oneline

Amend Commit Without Changing Message

StepCommand
Stage new changesgit add .
Update previous commitgit commit --amend --no-edit
Check commit historygit log --oneline

Change Author of the Last Commit

StepCommand
Update commit authorgit commit --amend --author="John Doe <john@example.com>"
Verify author changegit log

Update Commit Timestamp

StepCommand
Update commit dategit commit --amend --no-edit --date=now
Verify commit timestampgit log --pretty=fuller

Fix a Pushed Commit

StepCommand
Stage missing changesgit add .
Amend commitgit commit --amend --no-edit
Push rewritten commit using git pushgit push --force-with-lease

Undo git commit --amend

StepCommand
View commit history changes using git refloggit reflog
Restore previous commit using git resetgit reset --soft HEAD@{1}
Restore commit completelygit reset --hard HEAD@{1}

Modify Older Commits (Alternative to Amend using git rebase)

StepCommand
Start interactive rebasegit rebase -i HEAD~3
Edit selected commitgit commit --amend
Continue rebasegit rebase --continue

General git commit --amend Reference

CommandDescription
git commit --amendModify 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-editAmend commit without modifying the message
git commit --amend --author="Name <email>"Update author information
git commit --amend --date=nowChange commit timestamp
git push --force-with-leasePush amended commit safely after history rewrite
git reflogView 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.

bash
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.

bash
git commit --amend

After 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.

bash
git log --oneline

This 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.

bash
git add <file>

For example:

bash
git add config.yaml

Add files to previous commit using git amend

Once the file is staged, amend the previous commit to include the new changes.

bash
git commit --amend

Git 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.

bash
git commit --amend --no-edit

This 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.

bash
git commit --amend --no-edit

This 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.

bash
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.

bash
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.

bash
git log

The 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.

bash
git commit --amend --no-edit --date=now

This keeps the existing commit message but updates the commit date.

Set custom commit date using --date

You can also manually specify a commit date.

bash
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.

bash
git log --pretty=fuller

This 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.

bash
git push --force-with-lease

The --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:

bash
git add config.yaml
git commit --amend --no-edit
git push --force-with-lease

This 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.

bash
git reflog

Locate 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.

bash
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:

bash
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.

bash
git rebase -i HEAD~3

This 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:

bash
git commit --amend
git rebase --continue

This 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.

bash
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.

bash
git add config.yaml
git commit --amend --no-edit

This 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.

bash
git add .
git commit --amend --no-edit

This 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.

bash
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?

The git 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 using git 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 using git 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. The git 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:

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.