2

As far as I understand, git reset --hard will update in the index and working directory to whatever the HEAD points to?

Git checkout -- will update in working directory to whatever that file state in the INDEX is?

So if is unstaged, then they will do the same thing (change the file in the working directory to its state in the HEAD/INDEX [which are the same state for that file])? But if it is staged then git reset --hard will do as above, but Git checkout -- will do nothing?

Jonny Shanahan
  • 351
  • 3
  • 13
  • 1
    check http://stackoverflow.com/questions/3639342/whats-the-difference-between-git-reset-and-git-checkout – Navneet Aug 26 '15 at 12:06
  • You might want to clarify your question, perhaps add an example. As formulated, it isn't very clear. – jub0bs Aug 26 '15 at 12:17
  • 5
    The biggest difference is that one works, the other does not. You cannot `reset --hard` paths: `git reset --hard ` produces `fatal: Cannot do hard reset with paths.` – user229044 Aug 26 '15 at 12:24
  • Yeah that is quite an important difference. I should have checked that. – Jonny Shanahan Aug 26 '15 at 12:33

1 Answers1

0

git reset --hard expects a revision/commit, not a pathspec.

Resets the index and working tree. Any changes to tracked files in the working tree since commit are discarded. [emphasis mine]

If no revision is given, it defaults to HEAD (the current commit)

In contrast git checkout, when called as git checkout [tree-ish] -- pathspec..., overwrites the files at the specified path(s) in the working directory and in the index with the content of the blob referenced from the given tree. If no tree is provided, i.e. git checkout -- pathspec..., the files in the working directory are overwritten with the current content stored in the index

Overwrite the contents of the files that match the pathspec. When the tree-ish (most often a commit) is not given, overwrite working tree with the contents in the index. When the tree-ish is given, overwrite both the index and the working tree with the contents at the tree-ish. [emphasis mine]

To mimic the behavior of reset --hard (without a revision), you can use git checkout HEAD -- pathspec....

Git 2.25 introduced git restore. To get the same behavior with the new command, you'd use git restore --staged --worktree -- pathspec... (or git restore -SW -- pathspec...)

knittl
  • 246,190
  • 53
  • 318
  • 364