Skip to content
Log In Sign Up info@muvcloud.com

Fix the Last Commit Message

git commit --amend -m "Fix login redirect after logout"

Without -m, Git opens your editor with the current message so you can change it. Make sure nothing is staged that you do not want in the commit: amend takes the staged changes too. Check with git status.

Add a Forgotten File

git add config/routes.php
git commit --amend --no-edit

--no-edit keeps the message as it is. The file becomes part of the last commit as if it had been there from the start. If you staged a file by mistake, unstage it before you amend; see how to unstage files in Git.

What Amend Really Does

Git commits cannot be changed. Amend builds a new commit with the same parent and puts the branch on it; the old commit is no longer on the branch. You can see it in the hash:

git log --oneline -1
git commit --amend --no-edit
git log --oneline -1

The two hashes differ even if you changed nothing (as long as the clock has moved on by a second), because the new commit gets a new committer date. This is why amend counts as rewriting history.

Change the Author

If the commit was made with the wrong name or address, set the right ones and reset the author:

git config user.name "Your Name"
git config user.email YOUR_EMAIL
git commit --amend --reset-author --no-edit

Replace YOUR_EMAIL with your address. Without --global, the setting applies only to this repository; the global setup is in how to install Git. You can also name the author directly:

git commit --amend --author="Your Name <YOUR_EMAIL>" --no-edit

Amend changes only the last commit. To fix older commits, use an interactive rebase, as in how to squash commits in Git, with reword or edit in place of pick.

If the Commit Was Already Pushed

Warning: after amending a pushed commit, your branch and the remote disagree, and a normal push is rejected. Decide first who else uses the branch.

  • Only you: push with git push --force-with-lease. It refuses if someone pushed in the meantime, so you do not overwrite their commits. Never use a plain --force for this.
  • Others have pulled it: do not amend. Make a new commit with the fix, or undo the commit with git revert.
git push --force-with-lease origin feature

Undo an Amend

The commit before the amend is still in the reflog. Move the branch back to it and keep the changes staged:

git reflog
git reset --soft HEAD@{1}

HEAD@{1} is where HEAD was before the last move, so it is the original commit if the amend was your last action. Read the reflog to be sure. After the soft reset, whatever the amend added is staged, and you can commit it separately with git commit.

Common Problems

  • The editor opened and you are stuck: it is usually Vim; type :wq to save or see how to save and quit in Vim.
  • An unwanted file was included: remove it with git reset --soft HEAD~1, unstage the file, and commit again.
  • Nothing to amend: a brand new repository has no commit yet; make the first commit normally.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations