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 pushAmend 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 pushAmend 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 pushThe scenario above will result in error: failed to push some refs, with the following hint:

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 --forceis dangerous, as anything you overwrite will be removed from the branch history, and you could overwrite your teammate’s changes. You should usegit push --force-with-leaseinstead.
Notes
- This is a very conceptual (read: non-technical) explanation of how 
--force-with-leaseworks, but it’s what matters ↩︎