Oscar the owl and Vershd logo
Menu

How to Git Commit Amend Effortlessly

Introduction

If you understand the git commit --amend command, and how to use it correctly, then you’ll have another powerful tool to help you use Git to its full effect. But while git commit --amend seems simple on the surface, if you don’t understand exactly what it does, you may run into some difficulties.

The whole purpose of Git is to keep a record – a history – of all your work. All your code. All your changes. All your efforts. But Git is also flexible. It lets you define how you’d like your workflow – and your project history - to look. And that includes the ability to rewrite that history. To amend your commits.

Because what if you made a mistake? A small error or a minor typo that you just want to fix? What if you forgot to add a file or miswrote the commit message? That’s where git commit --amend comes in.

A Word of Warning

Using git commit --amend creates changes to your working directory. If you don’t know exactly what you’re doing, you may lose content. Make sure you understand the commands and consequences before you use it.

What is git commit --amend?

It's a way to rewrite history. It changes the most recent commit, the last one you made. It’s a useful way to undo any changes made by mistake, add files you forgot to include, or amend a commit message.

When used correctly, it’s good practice, because if you make a new commit to undo a change you previously made – making a change to a change – then your repository history could become a bit messy and unclear.

Using git commit --amend lets you make changes seemingly without having to create another commit. Technically though, you are making a new commit, with its own commit hash, to replace the commit you want to alter. That’s important to remember.

Also, the amend command only works on your most recent commit. If you want to edit any older commits, you’ll need to use another method to rewrite your Git history, rebase.

You’ll find that this command is useful in a number of scenarios, usually when you’ve just committed a bunch of changes but realised that you forgot a file. Or maybe you just entered the wrong commit message.

How to Use It

To add new staged changes to a previous commit

Simply add the files you forgot and run the git commit --amend command. You’ll be prompted to add a new commit message and the changes will merge with the previous commit. Remember, everything in staging will be committed when you use this command.

To amend the message on a previous commit

Just run git commit --amend without adding any new changes. The text editor will then ask you to enter a new commit message. Simple.

To remove that middle step, you can also just run the command: git commit --amend -m “Your new message”

To amend the commit without changing its message

If you are simply correcting a very minor mistake and don’t want to change your commit message, then use the command: git commit --amend –no-edit

Amending a Commit After a Push

In an ideal situation, you’ll realise any errors you made with your commit and use the amend functionality to fix them before you push the commit to the remote repository.

If you have pushed the commit, and you want to amend, then proceed with extreme caution.

This is because when you use the git commit --amend you are creating a new commit. That previous commit will no longer be on your branch. If your teammates have fetched that commit from the repo before you amend, and made commits themselves, they and you might run into problems that can be very difficult and time consuming to resolve.

If you’ve pushed a commit that you want to amend, remember: You’re not just rewriting your version now, you’re rewriting a version that others might have.

If you are confident amending a pushed commit won’t cause any problems, then you’ll need to use the --force or -f command. After making amends to the commit, enter: git push --force repository-name branch-name

If you’re not sure, you might want to try --force-with-lease first, as if there have been any upstream changes to the repository, this will abort the push. So enter: git push --force-with-lease repository-name branch-name

Zero stress Git

Vershd is an effortless Git GUI that boosts software development. It works on Windows, macOS, and Linux. It's free for personal, educational, non-profit and trade union use.

Designed for intermediate and beginner Git users, our unique UI/UX boosts your software development and makes Git simple.

Help & tools

We provide these pages to try to make your programming life easier. Our resources page gives an overview. You can see a full list of our Git tips & tools here.

We have these free tools:

Customers say...

I love any tool that makes my life easier... This is perfect, just the right amount of control. No more, no less. Easy to get started, push and rollback changes... It's a no brainer!
Hayden T.
Oscar the owl and Vershd logo
 © 2021 Blightysoft
Made in England
St. George's flag of England, the Union Jack flag of the United Kingdom, the European Union flag, and the United Nations flag.
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram