4 ms·
It is also really absurd that you can reset everything with `git reset --hard` but you have to use `git checkout -- ` to reset individual files. It's not even c
by ajkjk 6y ago
It is also really absurd that you can reset everything with `git reset --hard` but you have to use `git checkout -- ` to reset individual files. It's not even clear why `checkout` is involved in resetting files at all.
- johnmaguire 6y ago`git reset --hard` resets your worktree to the HEAD state. `git checkout -- <file>` checks out the HEAD version of a given file.
- infogulch 6y agoYes, but you can also word it in two other ways that make the redundancy more apparent at the cost of a tiny bit of inaccuracy: A: `git reset --hard` checks out the HEAD version of your worktree. `git checkout -- <file>` checks out the HEAD version of a given file. B: `git reset --hard` resets your worktree to the HEAD state. `git checkout -- <file>` resets a file to the HEAD state. I don't think a newbie could even grok the difference between these three descriptions.
- gpanders 6y agogit reset --hard does not only modify the work tree though, it also updates where your current branch points to. In fact, that is the main purpose of ‘git reset’, the fact that it modified your work tree at all is only because of the ‘--hard’ flag. So it’s not correct to say that “reset --hard and checkout do basically the same thing”.
- aidenn0 6y agogit reset --hard HEAD Does in fact only modify the work tree, since updating the current branch to HEAD is a nop.
- johnmaguire 6y agoYou could word it that way, but it's not actually correct. Check the man pages to see why. (Or read the other commenter's note which gives a brief explanation.) > I don't think a newbie could even grok the difference between these three descriptions. This part is definitely true - Git offers only a very light abstraction on top of its data structures.
- wereHamster 6y agogit-reset - Reset current HEAD to the specified state git-checkout - Switch branches or restore working tree files reset works primarily to modify HEAD, and when you include `--hard` it's like saying: oh and btw also update the working tree. checkout works primarily (in the case of checkout <pathspec>) to copy file from somewhere (commit, index) into the working tree. You can use checkout to mimic a hard reset: `git reset --hard` is pretty much equivalent to `git checkout HEAD .`
- ajkjk 6y agoI understand how it works, but I still think it's dumb.
- wereHamster 6y agoIs it perhaps because most people are taught about `reset --hard` early on? If all they knew was `checkout` there wouldn't be any confusion. Nobody really needs to know about `reset --hard`, the same effect can be achieved with a normal reset and a checkout, it's a two step process anyway, regardless of how you do it. Using reset+checkout is not the quickest but gets the job done. Power users can create themselves a shell alias, or write themselves a small shell script, or learn about one of the thousands commandline switches that does just what they need.
- veilrap 6y agoFor me, checkout is easy to remember because I'm literally checking out an older version of the file.
- striking 6y agoThe future is `git restore` and `git switch`, which clear up the ambiguity between different modes of reset/checkout.
- chousuke 6y agoThe logic is that you checkout the old file from the repository into your worktree, overwriting it. If there's something inconsistent about this, it's that it works for individual files, but (fortunately) not for clobbering your entire worktree by checking out a branch.