Compare the three git reset command levels (--soft, --mixed, and --hard), and what is the fundamental difference between Reset and Revert?
git reset moves the branch pointer to a previous commit with differences in where changes are saved: --soft moves modifications to the Staging Area, --mixed (default) moves modifications to the Working Directory and unprocesses them, and --hard permanently clears modifications from all locations. The key difference: Reset rewrites history by deleting commits and is only valid for local branches, while Revert creates a new commit that completely safely mirrors and undoes the impact of a previous commit for shared global branches.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
git reset levels table:
| The command | HEAD index and branch | Preparation area (Index / Stage) | Working Tree folder |
|---|---|---|---|
git reset --soft <hash> |
Moves backwards | Modifications remain ready | The modifications remain |
git reset --mixed <hash> (default) |
Moves backwards | Equipments are canceled (Unstaged) | The modifications remain |
git reset --hard <hash> |
Moves backwards | Modifications are deleted | Modifications are completely deleted |
When do you use Revert versus Reset?
- Local Feature Branch: Use
git resetif you want to undo your recent commits and refactor them cleanly. - Shared
mainor Production): Usegit revert <commit-hash>exclusively:# Creates a new commit that accurately undoes changes to the previous commit without touching the old history git revert 3a4b5c6
- Using git reset --hard on a branch that has been pushed to a remote repository and then trying to force a push can wipe out the team's work.
- The belief is that git reset --hard deletes everything irreversibly; lost commits can often be recovered via git reflog if the repository is not cleaned via Garbage Collection.
- How do you use the modern git restore command to undo modifications to a specific file instead of using the old git checkout?
- What happens if I git revert a Merge Commit?
What is the case of Detached HEAD, and how can git reflog rescue lost commits after a false reset?
Detached HEAD state means that the HEAD pointer points directly to a specific commit hash instead of pointing to a branch name. If you write new commits in this state and then move to another branch, those commits become suspended (Orphaned/Unreachable). The git reflog records every move the HEAD pointer made locally, enabling you to find the missing hash and create a new branch that installs it immediately.
Detailed Technical Explanation & PracticeExpand DetailsCollapse Details
Rescue scenario via git reflog:
- You made a critical commit in
Detached HEADstate or you did a wronggit reset --hard. - The commit disappeared from
git log. - Run the reference transaction log:
The output will show previous cursor movements:git reflog -n 10a1b2c3d HEAD@{0}: reset: moving to HEAD~3 f4e5d6c HEAD@{1}: commit: important emergency fix 9876543 HEAD@{2}: checkout: moving from main to detached HEAD - Create a new branch that installs this commit immediately before the memory cleaner deletes it:
git branch recovery-branch f4e5d6c
Now all files and commits have been recovered successfully and safely.
- Moving to another branch (such as git checkout main) after committing modifications is in Detached HEAD state without naming the branch, making modifications invisible in git log.
- Belief that git reflog is available on a remote server; the reflog is a purely local personal log and is not uploaded to GitHub.
- How long does the default log remain in git reflog before git gc deletes it?
- How do you recover a local branch that was accidentally deleted via git branch -D command?