6. Undoing Changes & Resetting History
Discarding local changes (git restore, git checkout)
Section titled “Discarding local changes (git restore, git checkout)”There will be times when you need to reset your working directory to a clean state. Whether it’s due to mistakes or a need to abandon changes, Git provides several commands to help you manage this cleanup.
One of the primary commands you’ll use is git restore. Introduced in Git 2.23, it split the file-restoring job out of the overloaded git checkout into a clear, purpose-built command, and it can act on both your working directory and your staging area. Think of it as the modern, less error-prone way to do what people used to do with git checkout <file> and git reset <file>.
To discard changes in your working directory, you can run:
git restore .This command will revert all modified and deleted files in the working directory to their last committed state. However, it will not affect the staging area, the zone where changes are prepared for committing.
If you only want to unstage files (i.e., move them from the staging area back to the working directory), use:
git restore --staged <file>Or if you need to reset the whole directory:
git restore --staged .To restore all files in both the staging area and the working directory to their state in the last commit, run:
git restore --source=HEAD --staged --worktree .A use case for restore
Section titled “A use case for restore”You can choose to restore a file that you previously deleted while still needing it:
git restore --source=HEAD <deleted-file>Here HEAD is just a pointer to the latest commit on the active branch, so --source=HEAD means “bring this file back to how it looked in the last commit.”
Unstaging files (git reset HEAD)
Section titled “Unstaging files (git reset HEAD)”The best way in the latest versions of Git to unstage files is to use the git restore --staged . command.
However, there is also the older command reset. This one can be quite confusing because it’s not always explicit about what is being reset.
To reset a staged file you can run:
git reset <file>If you need to reset all staged files you can run:
git resetThis will unstage all your files but your changes will still be there (you will not lose your code).
Reset —soft and —hard
Section titled “Reset —soft and —hard”When you need to undo a recent commit, there are different options available depending on whether you want to keep or discard the changes. Press the buttons below to compare them side by side:
HEAD points at C4, the latest commit. Your working directory is clean.
In most cases, you’ll want to reset your recent commit to either amend the commit message or because the content wasn’t ready, but still keep your staged files and local changes. In other words, you want to reset only the commit history.
To do this, you can use the following command:
git reset --soft HEAD~This command is elegant because it moves your HEAD back by one commit (i.e., resetting the erroneous commit you just made) while keeping your changes intact. Your working directory and staging area remain as they were, so you can modify the files or update the commit message before making a new commit. The ~ here is shorthand for ~1 (one commit back); HEAD~2 would move back two commits, and so on.
If you need to reset your latest commit and discard all changes made since then (for example, to undo an erroneous merge), you can use:
git reset --hard HEAD~This will move your HEAD back by one commit and completely discard all changes in both the working directory and the staging area since that commit.
Amending commits (git commit —amend)
Section titled “Amending commits (git commit —amend)”Sometimes, you don’t need to reset an entire commit just to fix a mistake. When you want to change a commit message or add a missing file, there’s no need to reset the entire commit history. Git provides a command for this called --amend.
If you run:
git commit --amendThis will allow you to edit your latest commit message. For example, you can fix a typo or adjust the commit message to be more accurate.
If you need to add or remove a file from the commit, follow these steps:
- Add the missing file:
git add <file>- Or, remove a file:
git rm <file>- Then, run the following to amend the commit without changing the message:
git commit --amend --no-editThe --no-edit flag tells Git that you don’t want to modify the commit message. Instead, it will update the commit to include the added or removed files.
The --amend command effectively rewrites the latest commit. It replaces the old commit in the history with the newly amended one. This means the original commit will no longer be visible after you amend it.
Reverting commits (git revert)
Section titled “Reverting commits (git revert)”Not all files in a project should be pushed to a remote repository. Some, like .env files and credentials, should always remain on your local machine. However, mistakes happen, and you might accidentally commit and push sensitive or unwanted files. In such cases, you need to revert the commit to remove the changes while keeping your commit history intact.
The advantage of using git revert is that it’s a non-destructive action. Instead of deleting or modifying the commit history, it creates a new commit that undoes the changes from the previous commit. This approach is recommended because rewriting history can cause issues for other contributors working on the project.
As a general rule, Git encourages avoiding history rewrites for commits that have already been shared with others. This same principle applies when we discuss rebasing in Chapter 8.
To revert a commit, use:
git revert <commit-hash>This command generates a new commit that negates the changes introduced by the specified commit. Once done, you can push the reverted commit to the remote repository so everyone stays up to date.
Reverting a merge commit needs one extra detail. A merge has two parents, so you must tell Git which one to treat as the mainline with -m (almost always -m 1, the branch you merged into):
git revert -m 1 <merge-commit-hash>Reset vs. Revert vs. Checkout
Section titled “Reset vs. Revert vs. Checkout”Git provides multiple ways to undo changes, and at first they can feel interchangeable. They are not. The key question is always: what exactly am I trying to undo, and has it been shared with others yet?
Let’s line up the three commands people most often confuse:
git checkout/git restoreworks on files. It discards uncommitted changes in your working directory and brings a file back to a previous state. It does not touch your commit history.git resetworks on the branch pointer. It moves your branch back to an earlier commit, optionally throwing away the changes in between. With--hardthis rewrites history and is destructive.git revertworks by adding history. Instead of erasing a commit, it creates a brand new commit that undoes the effect of an earlier one. Nothing is rewritten, which makes it safe to share.
| Feature | git checkout / git restore | git reset --hard | git revert |
|---|---|---|---|
| Operates on | Files in the working directory | The branch pointer (commit history) | The branch, by adding a new commit |
| Effect | Discards local file changes | Moves the branch pointer back and erases commits | Creates a new commit that undoes changes |
| History rewrite | No (history untouched) | Yes (destructive) | No (safe) |
| Use case | Drop edits you haven’t committed | Undo local commits before pushing | Revert changes safely in a shared repository |
| Impact on remote | None | Force push needed only if the commits were already pushed | Does not require force push |
When to use each
Section titled “When to use each”- Use
git restore(orgit checkout <file>) when you just want to throw away uncommitted edits to a file. - Use
git reset --hardonly when you haven’t shared your changes yet (e.g., fixing local commits before they leave your machine). - Use
git revertwhen you need to undo a commit while keeping history intact. This is the safer option, especially when working in a team.
By understanding these differences, you can choose the right approach based on your situation and avoid unnecessary issues in collaboration.