Sub topic 15
Unstaging Files and Amending the Last Commit
This chapter assumes you can create a repository, stage files with git add, and make commits with git commit. It also assumes you can read git status output. Here you will fix two common mistakes: staging the wrong thing, and committing with a bad message or a missing file.
Unstaging with git restore --staged
Suppose you edited README.md and staged it, but you want that change in a later commit. You can remove it from the staging area without touching the file itself.
git restore --staged README.mdThis copies the version from the last commit back into the staging area. Your working tree is unchanged, so your edits are still in the file. Run git status afterward and the file moves from "Changes to be committed" to "Changes not staged for commit".
On older Git versions (before 2.23), the equivalent command is git reset HEAD README.md. You will see it in older guides.
Amending the most recent commit
git commit --amend replaces the latest commit with a new one. You can use it to fix a message:
git commit --amend -m "Add README with usage notes"You can also use it to add a forgotten file or change. Stage what you forgot, then amend:
git add notes.txt
git commit --amend --no-editThe --no-edit option keeps the existing message. If you leave out -m and --no-edit, Git opens your editor with the old message so you can change it.
Why amend rewrites history
A commit's ID is computed from its contents, its parent, its author data, and its message. Change any of these and you get a different ID. So --amend does not edit the old commit. It builds a new commit and moves the current branch to point at it. The old commit is left unreferenced.
This is safe while the commit exists only on your machine. If you have already shared the commit, for example by pushing it, teammates may have built on the old version. Amending then forces you to rewrite shared history. Only amend commits that nobody else has.
Common errors
- Amending with nothing staged and the wrong message.
git commit --amendwith no changes still rewrites the commit, so a typo in-mreplaces your good message. - Amending the wrong commit.
--amendonly affects the latest commit. To change an older one you need interactive rebase, which is outside this chapter. - Expecting unstaging to undo edits.
git restore --stagedonly changes the staging area.
| Goal | Command |
|---|---|
| Remove a file from the next commit, keep edits | `git restore --staged <file>` |
| Discard edits in the working tree | `git restore <file>` |
| Fix the last commit message | `git commit --amend -m "new message"` |
| Add a forgotten file to the last commit | `git add <file>` then `git commit --amend --no-edit` |
Exercise
You commit with the message "Add loging", then notice the typo and also realize you forgot to stage config.txt. The commit has not been shared. What two commands fix both problems in one commit?
Show answer
git add config.txt followed by git commit --amend -m "Add logging". The amend creates one new commit containing the file and the corrected message.
Exercise
You ran git add . and staged debug.log by accident. Which command removes it from the staging area without deleting the file, and what should you add to prevent it happening again?
Show answer
git restore --staged debug.log unstages it and leaves the file on disk. Add *.log to .gitignore so Git stops offering it for staging.