1

Recently I messed up a commit was told to use this command, I am not 100% what it did, seemed very similar to a hard reset but I am not sure it's the same. I could not find a reference in docs.

I could not find how this What do git checkouts really mean? addressed the question, in the event it does please point me to where it does.

peterh
  • 11,875
  • 18
  • 85
  • 108
Michael
  • 4,538
  • 5
  • 31
  • 58

1 Answers1

3

It's a command of the form git checkout <pathspec>.

What it does is essentially to throw away uncommitted changes in the working tree: It overwrites all files in the given path (here ., the current directory, and all of its contents, recursively) with what is in the "index" (which is the latest commit + all changes added by git add, if any).

A hard reset does not just throw away your local changes, it also throws away committed history. git reset --hard FOO resets the state of the repository to FOO and makes it as if any commit after FOO never happened. You rarely need to use this.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Afaik it would be `git checkout -- .`. `git checkout .` would switch to a branch/tag named "`.`". – peterh Aug 04 '19 at 11:59
  • @peterh The `--` is optional. – melpomene Aug 04 '19 at 12:00
  • @peterh: note that `git checkout x` first tries to treat `x` as a *branch name*. If that succeeds, it means "check out branch `x`". If that fails, `git checkout` tries to treat `x` as a commit specifier (a "commit-ish", in Git terms). If that succeeds, it means "check out the commit specified by `x`"—so if `x` is a *tag*, you get a "detached HEAD". Last, if both of these have failed, it tries to treat `x` as a pathspec, as if you'd entered `git checkout -- x`. So if `.` could be a branch or tag name, this could behave differently; but `.` cannot be a branch or tag name. – torek Aug 04 '19 at 17:00