On Git Reset
3 min readMar 26, 2021
usecase
The concern is documenting the conceptualization of git reset
- NOTE: POTENTIALLY DESTRUCTIVE
These notes are from Online Git Course: Mastering Git @ Pluralsight
1. why confusing?
- you need to understand both the 3 main areas of git (work, index, repo) and the branching
- it has many usecases
2. what does it do?
2.1. in general: 4 ways of moving a branch
- commit
- merge
- rebase
- pull
- however, NONE OF THEM IS A SPECIALIZED COMMAND TO MOVE BRANCHES explicitly
- these 4 general ways to move a branch is a side-effect of the commands aimed at something totally different
2.2. enter git reset: fifth & specialized way to move a branch
- usually current branch
- pick a branch
- run
git reset
- that branch becomes the current commit → the branch is taken “back in time”
- notice that HEAD is not moved — still pointing to the same branch. Branch is moving
2.3. confusing part and 3 types of reset: hard, mixed, soft
- not what it does to the repo
- what it does to the remaining 2 areas
2.3.1. git reset –hard
- git reset — hard HEAD → going back to HEAD
- git reset — hard HEAD^ →going back to the commit before HEAD
- git reset — hard HEAD~1 → equivalent to “^”
- git reset — hard HEAD~2 →going back two commits before HEAD
- usecase → committed in the wrong branch
2.3.2. git reset –mixed
- or just
git reset
or - if
--mixed
→ this is the default option
git reset
therefore undoes the staging of files
2.3.3. git reset –soft
- if
--soft
→ touch neither index nor working area - just move the branch in a repo and leave index and working area untouched
3. how can you lose files? don’t commit your changes & run git reset –hard
- this seems to be irreversible (just done it by mistake and it hurts)
- you commit a file
- you change the file in the working area and do not commit it
- you run
git reset --hard
- the branch is moved to the HEAD, and the state of that move is copied both to the index and the working area
- if you made any changes to either work or index, that’s all irreversibly lost
- rule: no commit, no restore