git reset
git reset
: reset the HEAD and current branch to a different commit.
git reset
is a command in Git used primarily to move the HEAD and the current branch back to a specific commit, effectively "rewinding" the project's history to a previous state. This command is useful when you need to undo commits that have been made to the current branch.
When you use git reset
, it's like telling your Git repository to forget that some changes ever happened, and to consider an earlier state as the current state of your project. This is extremely useful for undoing changes, simplifying complex history, or even combining multiple commits into one before sharing them with others.
Historically, git reset
was used for various purposes, including resetting the working tree and staging area. To this day, veteran developers still use git reset
for these tasks. However, with the introduction of the git restore
command in Git 2.23.0, it is now recommended to use git restore
for managing the working tree and staging area, while git reset
should primarily be used for moving the HEAD and current branch pointer.
When using git restore
, you are less likely to accidentally break things, as it's more targeted and less destructive than git reset
. However, for someone who is used to the old git reset
command, it might take some time to get used to the new command. But if you're new to Git, it's a good idea to understand the difference and try to use git restore
instead of git reset
when appropriate.
Examples
Move the current branch pointer to a specific commit. This will keep the changes in the working directory, so you can modify and commit them again if needed. Useful for modifying an existing commit:
git reset a1b2c3d4
Move the current branch pointer to a specific commit, and reset the staging area to match, but not the working directory. Useful for keeping changes you've made but starting a new commit:
git reset --soft a1b2c3d4
Move the current branch pointer to a specific commit, and discard all changes in the staging area and working directory. Useful for completely discarding uncommitted changes. Be careful: this action cannot be undone!
git reset --hard a1b2c3d4
Instead of a commit hash, you can also use relative references: move the current branch pointer one commit back:
git reset HEAD~1
Be careful when using git reset
, especially with the --hard
option, as it can permanently discard changes. Always double-check that you're resetting to the correct commit, and consider using git stash
to save your uncommitted changes before resetting if you think you might need them later.
Note that git reset
doesn't remove commits entirely; it actually just moves the branch pointer. The "removed" commits are still in the repository's history (accessible via their commit hash), but they're no longer reachable from the current branch. This means that you can recover them if needed, but they won't show up in the branch's history anymore.
Hi! I'm Alex, creator of GitByBit.
And this is Gitopedia—a reference that summarizes knowledge in my Git course.
Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:
Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.