Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Reset


Share

What Does Git Reset Do?

The git reset command moves your current branch (HEAD) to a different commit.

Depending on the option, it can also change which changes are staged or even delete changes from your working directory.

Use it to undo commits, unstage files, or clean up your history.


Summary of Git Reset Commands and Options

  • git reset --soft <commit> - Move HEAD to commit, keep changes staged
  • git reset --mixed <commit> - Move HEAD to commit, unstage changes (default)
  • git reset --hard <commit> - Move HEAD to commit, discard all changes
  • git reset <file> - Unstage a file
  • git log --oneline - Show commit history

How to Find the Commit to Reset To

First, you need to find the commit you want to go back to.

Use git log --oneline to see a summary of your commit history, then move the branch back to that commit:

After the previous chapter, we have a part in our commit history we could go back to.

Let's try and do that with reset.


Git Reset Find Commit in Log

First thing, we need to find the point we want to return to.

To do that, we need to go through the log.

To avoid the very long log list, we are going to use the --oneline option, which gives just one line per commit showing:

  • The first seven characters of the commit hash - this is what we need to refer to in our reset command.
  • the commit message

So let's find the point we want to reset to:

Example

git log --oneline
e56ba1f (HEAD -> main) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/main) 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/main) Merge branch 'main' 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!
Try it Yourself »

We want to return to the commit: 9a9add8 (origin/main) Added .gitignore, the last one before we started to mess with things.



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
Try it Yourself »

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
Try it Yourself »

All changes after 9a9add8 are now unstaged, but still in your files.


Git Reset --hard

git reset --hard <commit> moves HEAD to the specified commit, and makes both the staging area and your working directory match that commit.

Warning: The commits after it are removed from the branch, and any uncommitted changes in tracked files are lost for good.

If you reset by mistake, git reflog can still find the removed commits.

Example

git reset --hard 9a9add8
HEAD is now at 9a9add8 Added .gitignore
Try it Yourself »

Unstage a File with Git Reset

git reset <file> removes a file from the staging area, but keeps your changes in the working directory. HEAD does not move.

Example

git add index.html
git reset index.html
Unstaged changes after reset:
M       index.html
Try it Yourself »

This does the same as git restore --staged <file>.


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.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.