1 minute read

NB: Don’t apply git reset without knowing details. First learn and apply in demo project then after getting familiar apply in real project.

Who doesn’t use git! But most of us using git without knowing some special command like git reset

In this blog, I will discuss some use cases of git reset command.

Undo a commit

After commiting something changes you find out you don’t need the commit yet. So you want the commit in unstaged mode. Let’s do it in git commad. Git reset command help you to do this.

# copy sha1-commit-id for the commit you want to undo
git log
# reset the commit to prevous commit
git reset --soft <sha1-commit-id>
# it will move your commit to staged mode.
git restore --staged commit_file

Delete a commit

If you want to delete a commit you can use --hard argument to do this. Although this is dangerous but while doing this you need to be careful.

git add .
git commit -m "this commit will get deleted with all info"
# copy previous commit sha1-hash-id
git reset --hard sha1-hash-id
# or you can move the head to previous commit by
git reset --hard HEAD~1
# the recent commit will get delete and previous commit will the HEAD

For more follow the beautiful atlassian blog

Categories: ,

Updated:

Comments