Oftentimes we commit some code and want to undo it for any reason (may be for retyping commit message or whatever). Git does not
leave us alone in this scenario and provides a reset command for this purpose.
With reset command we can undo changes or we can totally destroy our commit. Second one is dangerous, so always think
twice before resetting anything in git.
Before proceeding I will briefly explain what happens when we commit. Git is all about taking
snapshot of file with time. When we add file in index or staging area and commit, git takes the snapshot of staging area
and stores it as blob in .git directory. With this, we have actually three place where a file can reside.
In working directory
In index/staging area
In git repo
git-reset has close relationship with these places. Possibly, we can do any of the following with reset command
Move changes from git to index ( index <- git )
Move changes from index to working directory ( working <- index )
Move changes from git to working directory ( working <- git )
Completely remove the changes (dangerous!)
Let’s create a demo repo for this article.
Now we have file post.txt in our gitdemo local repo. post.txt is newly created file and is in working directory. Let’s
add/remove it to index.
add post.txt to index
By default, reset will unstage changes or will move the changes from repo to working directory. Default reset is same
as reset --mixed
working area <- staging
working area <- repo
Soft reset moves the changes from git repo to staging area. git reset --soft commit-hash
stating area <- repo
Hard reset completely removes the changes made by a commit. Once you hard reset a commit, you will not be able to recover changes.