The git reset
command is a powerful tool in Git's suite, allowing developers to undo changes to their project's history and staging area. This command can be intimidating at first as it is one of the few commands that is destructive and can cause you to lose work.
However, understanding how to use git reset
effectively can get you out of tricky situations and undo other mistakes. This guide will explain the various git reset
modes (hard, soft, and mixed), what they do, and when to use them.
**Understanding git reset
git reset
: At its core,git reset
changes the current HEAD to the specified state. It can modify the index (staging area), the working directory, and which commit the tip of the current branch is pointing to, depending on the mode used.
**Modes of git reset
- Hard reset (
-hard
): This mode resets the index and working directory.
Usage:
git reset --hard <commit>
Before resetting to a specific commit it’s helpful to look at the git log to see the current state of your Git history. Here you can see where you are in the timeline, grab specific commit hashes, and more.
Git hard reset examples:
Reset to a specific commit:
git reset --hard <commit-hash>
Reset to the previous commit:
git reset --hard HEAD^
Reset to the 3rd commit before the current HEAD:
git reset --hard HEAD~3
Reset the current branch to match another branch:
git reset --hard origin/master
Soft reset (
-soft
): This mode only resets the current branch's tip to the specified commit but does not touch the index or working directory. It's as if you undid your last commit(s) but left your files staged.Usage:
git reset --soft <commit-hash>
Mixed reset (default): This is the default mode when no option is specified. It resets the index but not the working directory. It un-stages changes but does not discard them. The changes will still be present in local memory.
Usage:
git reset <commit-hash>
Git Resetting HEAD
git reset HEAD
: This command is often used to unstage files that have been added to the staging area by mistake.
Practical uses and examples
Reverting changes: If you want to discard all changes in the working directory since the last commit, use:
git reset --hard
Undoing a git commit: To undo the last commit and keep the changes available in the local working directory run:
git reset --soft HEAD^
Cleaning the working directory: To remove all changes in the working directory:
git reset --hardgit clean -fdx
By using git clean you will remove all untracked files from the working tree.
Advanced resets
Reset to a remote branch: If you need to reset your local branch to exactly match a remote branch:
git fetch origin git reset --hard origin/<branch-name>
Resetting a file to HEAD: To reset a specific file to its state at HEAD:
git checkout HEAD -- <file-path>
Conclusion
The git reset
command is versatile, offering different modes for various scenarios. Whether you need to discard changes, undo commits, or clean your working directory, understanding how to use git reset
effectively is crucial for managing your Git repository. Always proceed with caution when using git reset --hard
, as it can permanently delete your work.