• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Git commit amend

Posted Jan 24, 2023 — Updated Jan 10, 2024

Maybe there’s a typo in your most recent commit message, or maybe you forgot to add a file. Git commit –amend allows you to go back and modify your commit—no one has to know you made a mistake.

Amend a previous commit message

Here’s a quick example fixing a typo in a message:

git add dev_file.py
git commit -m "adds logc to dev_file"

# looks like I missed a key typing 'logic' above, let's fix it
git commit --amend -m "adds logic to dev_file"
git push

Amend the contents of a commit

Amending a commit without editing the message:

git add dev_file.py
git commit -m "adds logic to dev_file and updates README"

# README was left off by accident
# Let's add it without changing the message from above
git add README
git commit --amend --no-edit
git push

Amend with a --no-edit flag will update the most recent commit without changing the commit message.

Pushing your edited commits (safely)

Under the hood, git commit amend actually creates an entirely new commit. So if you amend a commit that has already been pushed, git will be confused about the state of the branch and it’ll throw an error:

git add dev_file.py
git commit -m "adds logic to dev_file and updates README"
git push

git add README
git commit --amend --no-edit
git push

The scenario above will result in error: failed to push some refs, with the following hint:

git's "failed to push some refs" warning messages
git hint when push is rejected

What git is really saying is “Your branch’s history looks different than I think it should, so I’m not letting you push.” This makes sense; if multiple people are pushing to a single branch, someone else might have updated it more recently than you, and your push could wipe out their code. If that’s the case, you should git pull and then try and push your commit again.

Git push –force-with-lease

However, if you know the code you’re trying to push is correct, you can force git to accept your push as the truth using git push --force. Git push --force is dangerous, though. It will force your commit and overwrite anything on the remote repository which is not on your local branch.

To be safe, you should use git push --force-with-lease. Force with lease behaves the same as --force in that it will overwrite the history, but first it checks the remote branch to make sure it’s not going to overwrite someone else’s work.1  I  do this so often I set up the alias git fwl for it.

Just to reiterate, because using --force can cause real problems:

git push --force is dangerous, as anything you overwrite will be removed from the branch history, and you could overwrite your teammate’s changes. You should use git push --force-with-lease instead.


Notes

  1. This is a very conceptual (read: non-technical) explanation of how --force-with-lease works, but it’s what matters ↩︎

Filed Under: Git

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy