What is the difference between "git reset --hard HEAD~1" and "git checkout ."?

231 Views Asked by At

I'm currently learning git, and I have issue what exactly do these commands do?

git reset --hard HEAD~1

git checkout .

What's the difference between the two? They both permanently delete the last commit, as I understand. I think that there is a deep difference, so I hope for a clear explanation because I'm a beginner.

2

There are 2 best solutions below

5
NubDev On

git reset --hard HEAD~1: Permanently removes the last commit and all uncommitted changes.

git checkout .: Discards changes in the working directory and restores it to the state of the last commit without affecting commit history.

0
Jay On
git reset --hard HEAD~1

makes your current branch/HEAD move to the previous commit.
--hard discards changes to tracked files, including staged changes.
(As this operation moves HEAD, an entry in git reflog is created)

git checkout .

discards unstaged changes from tracked files in the current directory tree.
This means: Staged changes will be kept on top of current commit!
To also discard staged changes you have to specify the commit to check out, e.g. HEAD: git checkout HEAD . - which will behave similar to git reset --hard HEAD (no ~1!) - if run in top directory of the repo.