-1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Does this answer your question? [What's the difference between "git reset" and "git checkout"?](https://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout) – Wyck Jul 03 '23 at 00:28

2 Answers2

1

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.

NubDev
  • 506
  • 11
  • Is `git reset --hard HEAD` more similar to `git checkout .`? – ikegami Jul 03 '23 at 04:36
  • 1
    No, `git reset --hard HEAD` affects the entire repository, resetting all changes in all files and commits. `git checkout .` affects only the current directory and its subdirectories, discarding changes in those specific files. – NubDev Jul 03 '23 at 04:47
  • 1
    You say no, but your explanation makes it sound like they're the same if `$PWD` is the repo root. – ikegami Jul 03 '23 at 05:09
  • 1
    yes in that case they are same. – NubDev Jul 03 '23 at 05:10
  • 1
    `git checkout .` will *not* discard staged changes. So it does not necessarily restore the working directory to the state of the last commit. – Jay Jul 05 '23 at 13:01
0
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.

Jay
  • 3,640
  • 12
  • 17