How do I fix a typo in my last commit?
You just made a commit. You see a typo in a file or in the commit message. You want to fix it and have it count as the same commit, not a new one.
There are two cases.
Case 1: Fix a typo in a file (and keep the same commit message)
- Fix the typo in the file. Save the file.
- Stage the fix:
git add the-file-you-fixedOr stage everything:
git add .- Change the last commit to include this fix:
git commit --amend --no-edit--no-edit means: keep the same commit message. Your fix is now part of the last commit. You still have one commit, not two.
Case 2: Fix the commit message (the text you wrote for the commit)
You don’t need to change any files. Just run:
git commit --amend -m "New message here"Replace “New message here” with the correct message. The last commit now has the new message.
If you already pushed that commit to GitHub
After you run git commit --amend, your history changed. So you have to push again with:
git push --forceOnly do this if no one else is using the same branch. Force push can cause problems for others. If you work alone on this repo, it’s fine.