Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Terminal Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Amend


Share

What is Git Amend?

Git Amend is a command that allows you to modify the most recent commit.

You can use it to fix typos, add or remove files, or change the commit message.


When to Use Git Amend

Use Git Amend when you need to make small changes to your last commit.

It's perfect for fixing mistakes, adding forgotten files, or updating the commit message.


Fix Last Commit Message

To change the last commit message, follow these steps:

  1. Open your terminal and navigate to your repository.
  2. Type git commit --amend -m "New message" to change the commit message.
  3. Press Enter to save the changes.

Example

git commit --amend -m "Corrected commit message"
[main bb9c969] Corrected commit message
 Date: Wed Jan 15 10:00:00 2025 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)
Try it Yourself »

Add Files to Last Commit

To add files to the last commit, follow these steps:

  1. Open your terminal and navigate to your repository.
  2. Type git add <file> to add the file to the staging area.
  3. Type git commit --amend to add the file to the last commit.
  4. Press Enter to save the changes.

Example

git add forgotten.txt
git commit --amend
Try it Yourself »


Remove Files from Last Commit

To remove files from the last commit, follow these steps:

  1. Open your terminal and navigate to your repository.
  2. Type git reset HEAD^ -- <file> to remove the file from the staging area.
  3. Type git commit --amend to remove the file from the last commit.
  4. Press Enter to save the changes.

Example

git reset HEAD^ -- unwanted.txt
git commit --amend
 1 file changed, 3 insertions(+), 1 deletion(-)
Try it Yourself »

Now let's check the log:

Example

git log --oneline
07c5bc5 (HEAD -> main) Adding plines to reddme
9a9add8 (origin/main) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/main) Merge branch 'main' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
Try it Yourself »

Oh no! The commit message is full of spelling errors.

Embarrassing. Let's amend that:

Example

git commit --amend -m "Added lines to README.md"
[main eaa69ce] Added lines to README.md
 Date: Thu Apr 22 12:18:52 2021 +0200
 1 file changed, 3 insertions(+), 1 deletion(-)
Try it Yourself »

And re-check the log:

Example

git log --oneline
eaa69ce (HEAD -> main) Added lines to README.md
9a9add8 (origin/main) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/main) Merge branch 'main' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
Try it Yourself »

We see the previous commit is replaced with our amended one!

Warning: Messing with the commit history of a repository can be dangerous.

It is usually ok to make these kinds of changes to your own local repository.

However, you should avoid making changes that rewrite history to remote repositories, especially if others are working with them.


Git Amend Files

Adding files with --amend works the same way as above.

Just add them to the staging environment before committing.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.