What Does git reset Do?
The git reset command moves your current branch (HEAD) to a specific commit.
Depending on the options you use, it can also:
- Keep or unstage changes in the staging area (index)
- Remove changes from your working directory
It’s commonly used to undo commits, unstage files, or clean up your commit history.
Common git reset Options
How to Find the Commit to Reset To
To choose the commit you want to reset to, run:
git log --oneline
This shows a list of recent commits, making it easy to copy the commit hash you want.
Find the Commit to Reset To
Before running git reset, you need to find the commit you want to go back to.
We do this by checking the commit history.
To make the history easier to read, use the --oneline option.
This shows each commit on a single line, including:
- The first few characters of the commit hash (which you’ll use in the reset command)
- The commit message
Example:
git log --oneline
Output:
sql
Copy
Edit
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
Choose the Commit
In this example, we want to go back to:
9a9add8 (origin/master) Added .gitignore
This is the last commit before the changes we now want to undo.
Git Reset --soft
git reset --soft <commit> moves HEAD to the specified commit, but keeps all your changes staged (in the index).
This is useful if you want to combine several commits into one, or just want to rewrite history but keep your work ready to commit.
Example:
git reset --soft 9a9add8
All changes after 9a9add8 are now staged, ready for a new commit.
Git Reset --mixed (default)
git reset --mixed <commit> (or just git reset <commit>) moves HEAD to the specified commit and unstages any changes, but keeps them in your working directory.
This is the default option and is useful if you want to "undo" a commit but keep your changes for editing or recommitting.
Example:
git reset --mixed 9a9add8
All changes after 9a9add8 are now unstaged, but still in your files.
Review Changes
After running Git Reset, review your changes to make sure everything is as expected.
Tips & Best Practices
- Use Git Reset with caution, as it can rewrite your commit history.
- Make sure to communicate with your team before making changes to the remote repository.
Troubleshooting
If you encounter issues with Git Reset, try using git status to see the current state of your repository.
Warnings
- Be careful when using Git Reset, as it can delete changes and rewrite your commit history.
- Make sure to use it only when necessary.