If you’ve ever worked with Git, whether as a developer or a tester, chances are you may come across the git reset command often suggested by Git Bash itself. But running this command without knowing exactly what it does can lead to unexpected results, including data loss.
In this blog, I will elaborate on when to use git reset and why and help you avoid potential pitfalls so you can do it in confidence. Whether you are unstaging files or undoing commits, mastering git reset is an essential skill for anyone working in software development or testing.
What is Git Reset?
The git reset command is used to undo changes at different levels in Git. It operates in two primary scenarios:
1. To remove changes from the staging area (unstage files).
2. To undo commits at the repository level.
____________________________________________________________________________________________
1. Removing Changes from the Staging Area
If you’ve mistakenly staged a file, you can use git reset to unstage it:
Command Sytax:
git reset <filename>
This removes the file from the staging area but leaves it untouched in your working directory.
Command Example:
git reset <filename> was used to unstage XLUtility.java:
This command removes the file from the staging area but keeps the changes in the working directory.
2. After resetting:
The file XLUtility.java moved to unstaged changes (red color).
Note: Below commands will show all the files in staging area
git ls-files
git status
___________________________________________________________________________________________________
2. Undoing Commits at the Repository Level
To undo commits, you first need to view the commit history:
git log --oneline
Then, you can use the git reset command to move the HEAD pointer back to a specific commit. This effectively "removes" all commits after the specified commit. The effect depends on the reset mode you choose.
Command Sytax:
git reset <mode> <commitID>
The mode determines whether changes will be removed from the staging area and/or the working directory.
Important Note:
You cannot delete individual commits by specifying their commit ID. Instead, you must reset to the commit before the one you want to remove, which will effectively discard that commit and all subsequent ones.
Reset Modes Explained
a) --mixed (Default Mode)
• Purpose: Removes commits and un-stages changes, but keeps them in the working directory.
• Use Case: When you want to discard commits but retain the actual file changes for further modifications.
• Key Feature: The changes remain in your working directory but are no longer staged for commit.
Command Sytax:
git reset --mixed <commitID>
Command Example:
1. git reset --mixed d02a8a3:
The --mixed option resets the commit history back to d02a8a3.
What happened?
The changes from c354ed8 (testing mixed mode) are removed from the commit history.
Changes remain unstaged (in the working directory).
2. git status after reset:
Unstaged files (modified, shown in red):
src/test/java/api/endpoint/Routes.java
src/test/java/api/endpoint/userEndpoints.java
src/test/java/api/utilities/XLUtility.java
Untracked file:
src/test/java/api/test/BasicAuth.java (needs to be added with git add).
Summary of git reset --mixed:
Commits after the specified commit are removed.
Changes remain in the working directory as unstaged.
You can re-add and commit specific files if needed.
___________________________________________________________________________________________________
b) --soft Mode Explained
• Purpose: Removes commits but keeps changes in both the staging area and the working directory.
• Use Case: When you want to adjust your commits but retain all staged changes for re-commit.
• Key Feature: The staging area remains untouched, allowing you to commit again directly.
Command Syntax:
git reset --soft <commitID>
Command Example:
git reset --soft d02a8a3
Here, 52408bd is latest commit, but let’s say I want to undo it and return to d02a8a3.
This will move HEAD pointer to the commit d02a8a3, but changes will still be staged.
Verify the Reset
Check the commit history again:
git log --oneline
we will now see below output:
d02a8a3 (HEAD -> master) last commit
7bea34d (origin/master) first commit
When to Use git reset --soft
You might want to use git reset --soft when:
You accidentally commit changes too soon.
You want to rewrite or squash multiple commits.
You realize your commit message was incorrect.
When NOT to Use git reset --soft
Refrain from using git reset if you've already pushed your commits to a shared branch. It alters history, which can lead to conflicts for others collaborating on the same branch. Instead, consider using git revert to safely undo commits.
Advantages of git reset --soft
Safe Undo: It leaves your working directory and staged changes untouched.
Convenience: Lets you rewrite commits while keeping your progress intact.
Flexibility: Ideal for correcting commit messages or merging several commits.
Conclusion
git reset --soft is a lifesaver when you need to undo commits without losing your work. It keeps your changes intact, allowing you to rewrite history smoothly. The next time you find yourself needing to correct your commit history, give git reset --soft a try.
______________________________________________________________________________________________
c) --hard Mode Explained
•Purpose: Completely removes commits and discards all changes from the staging area and working directory.
•Use Case: When you want to discard all changes permanently and reset to a clean state.
Command Sytax:
git reset --hard <commitID>
Warning: This mode permanently deletes changes. Be cautious when using it, as there’s no way to recover changes once removed.
Command Example:
View Commit History
1.Before the reset:
git log –oneline
2.Hard Reset to a Previous Commit
To reset to d02a8a3 and discard all changes
git reset --hard d02a8a3
3.Verify the Reset
Check the log again:
git log –oneline
Output: The commit 8771d85 has been removed, and all changes made in that commit are lost.
When to Use git reset --hard
1. You are completely sure that you want to remove all changes.
2. You require a fresh start after trying something out or dealing with disorganized commits.
⚠️ Caution: After running this command, any discarded changes cannot be retrieved unless they were previously committed.
___________________________________________________________________________________________________
Conclusion
To help you learn and improve your work, the command git reset is incredibly useful. However, caution when using it—especially with the --hard option, as it can permanently erase any changes you've made. By understanding how git reset functions and its different modes, you can effectively incorporate it into your Git workflow. Keep in mind that if you're uncertain about the effects of git reset, you might want to consider safer alternatives like git restore or git revert, particularly in team settings.