Undoing Local Changes That Have Not Been Committed
When you've made changes to your working directory that you want to discard before committing, Git provides a straightforward way to revert individual files to their last committed state. This is particularly useful when experimental changes haven't worked out or when you've accidentally modified files.
- In your terminal (Terminal, Git Bash, or Windows Command Prompt), navigate to the folder containing your Git repository.
- Run git status to see which files have been modified. The affected files will be listed under "Changes not staged for commit."
- Execute the following command, replacing filename.html with the actual file path (you can copy and paste this directly from the git status output):
- git checkout filename.html
- The specified file has now been reverted to its state at the previous commit, effectively discarding all uncommitted changes to that file.
Git Checkout Process
Navigate to repository
Open terminal and navigate to your Git repo folder
Check file status
Run git status to see which files have been modified
Revert specific file
Execute git checkout filename.html to restore the file to previous commit state
Use git checkout for uncommitted changes when you want to discard local modifications and return a file to its last committed state. This is perfect for experimental changes you decide not to keep.
Undoing a Specific Commit (That Has Been Pushed)
When you need to undo a specific commit that's already been pushed to a remote repository, the safest approach is to use git revert. This creates a new commit that reverses the changes from the problematic commit, maintaining a clean project history without rewriting existing commits that others may have already pulled.
- In your terminal, navigate to your Git repository folder.
- Run git status to ensure you have a clean working tree with no uncommitted changes.
- Every commit has a unique hash identifier (typically displayed as a shortened 7-character string like 2f5451f). Locate the hash for the commit you want to undo using either method:
- View the commit history on GitHub, GitLab, Bitbucket, or your preferred Git hosting platform.
- In your terminal, run git log --oneline to see a condensed list of recent commits with their hashes.
- Once you have the target commit hash, execute the following command (replace 2f5451f with your actual commit hash):
- git revert 2f5451f --no-edit
- NOTE: The --no-edit flag automatically uses Git's default commit message, avoiding the need to enter a text editor. Without this flag, Git will open your default editor (often Vim). If you find yourself in Vim unexpectedly, exit by pressing Escape, then typing :q! and pressing Enter.
- Git creates a new commit that applies the inverse of all changes from the target commit, effectively undoing those changes while preserving the project's commit history.
- If you're working with a remote repository, push the revert commit to make it available to other team members:
- git push
Finding Commit Hashes
| Feature | GitHub/Bitbucket Web | Terminal Command |
|---|---|---|
| Access Method | Web browser interface | Command line |
| Command Required | None | git log --oneline |
| Information Shown | Full commit details | Condensed hash and message |
If you forget the --no-edit flag, you'll enter VIM editor. Exit by pressing colon (:), then 'q' for quit, then hit Return/Enter. This prevents getting stuck in the editor.
Undoing Your Last Commit (That Has Not Been Pushed)
Sometimes you'll commit changes only to immediately realize you've made an error—perhaps you forgot to include a file, made a typo in the commit message, or want to reorganize your changes differently. When the problematic commit hasn't been pushed yet, you can cleanly undo it while keeping your changes intact, allowing you to make a corrected commit.
- Navigate to your Git repository folder in your terminal.
- Execute this command to undo your last commit:
- git reset --soft HEAD~
- TIP: To undo multiple recent commits, add a number to specify how many commits to reverse. For example, git reset --soft HEAD~2 undoes the last 2 commits.
- NOTE: git reset --soft HEAD~ and git reset --soft HEAD^ are equivalent commands—you may encounter either syntax in Git documentation.
- Your most recent commit is now undone, but your changes remain intact and staged (as if you had run git add on them). This allows you to make additional modifications, add forgotten files, or reorganize your changes before creating a new, corrected commit.
Git Reset Options
Soft Reset
Undoes commit but keeps changes staged. Files remain ready for a new commit with modifications intact.
Multiple Commits
Add numbers to undo several commits at once. For example, HEAD~2 undoes the last two commits while preserving changes.
git reset --soft HEAD~ is the same as git reset --soft HEAD^Undoing Local Changes That Have Been Committed (But Not Pushed)
When you have multiple local commits that you want to completely eliminate—perhaps an experimental feature branch that didn't work out, or a series of commits with errors—you can reset your repository to a previous good state. This approach effectively erases the unwanted commits from your local history, but should only be used for commits that haven't been shared with others.
- Open your terminal and navigate to your Git repository directory.
- Run git status to confirm you have a clean working tree with no uncommitted changes.
- Identify the commit hash for the last good commit (the state you want to return to). You can find commit hashes in two ways:
- Browse the commit history on GitHub, GitLab, Bitbucket, or your Git hosting platform's web interface.
- In your terminal, run git log --oneline to display a chronological list of commits with their abbreviated hashes.
- Once you have the target commit hash, choose one of these reset commands (replace 2f5451f with your actual commit hash):
- git reset 2f5451f
- git reset --hard 2f5451f
- IMPORTANT: git reset removes the commits but preserves the changes as uncommitted modifications in your working directory. This is the safer option since you retain access to the code and can selectively incorporate useful parts into new commits. git reset --hard completely discards both the commits and all associated changes—use this only when you're certain you want to permanently delete the work.
Git Reset vs Git Reset Hard
Always ensure you have a clean working tree before performing reset operations. Run git status to verify no uncommitted changes exist that could be lost during the reset process.
Git Reset Process Flow
Check Status
Verify clean working tree with git status
Find Hash
Locate target commit hash via web interface or git log
Execute Reset
Run git reset or git reset --hard with commit hash
Grow Your Skills
Mastering Git is essential for modern software development workflows. Whether you're collaborating on enterprise projects or managing personal code repositories, these version control skills will enhance your productivity and confidence as a developer. Expand your technical expertise with our comprehensive programming courses designed for working professionals:
Course Categories Available