1

I've tried:

git checkout c3e715e -- file
git reset c3e715e -- file

Both times, if I git log afterwards, I still see the log as if it never reset.

doctopus
  • 5,349
  • 8
  • 53
  • 105
  • 2
    `git log` shows the commits you have made, neither of those commands creates a new commit. Check the file contents, they should have been changed to the state of commit `c3e715e`. – mkrieger1 Jul 27 '20 at 13:06
  • 1
    @Liam Because they want to reset a single file. See [How can I reset or revert a file to a specific revision?](https://stackoverflow.com/q/215718/4518341) – wjandrea Jul 27 '20 at 13:30
  • 2
    Moderators, not sure if the reasons of closing this question are proper. One reason is the "Duplicate", that leads to a wrong link, and the second reason is "Not reproducible or was caused by a typo" which seems incorrect as well. Could you please revisit your closing votes and do adjustments? (or provide more details) – Just Shadow Jul 27 '20 at 13:36
  • IMO This is either a duplicate or the OP needs to clarify their question @JustShadow – Liam Jul 27 '20 at 13:46
  • VTR - this is not a duplicate of [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/q/4114095/4518341) or even [How can I reset or revert a file to a specific revision?](https://stackoverflow.com/q/215718/4518341) It's a misunderstanding that resetting doesn't create a commit. – wjandrea Jul 27 '20 at 18:46

2 Answers2

4

The "--" is not needed needed.
Just do checkout by providing the file name:

git checkout c3e715e filename

Also please make sure that you don't have any pending changes on that file before running the command.


Some notes (just in case you're interested)

There is a slight difference between checkout and reset.
Regarding the reset:

When invoked with a file path, git reset updates the staged snapshot to match the version from the specified commit.

In case of checkout:

Checking out a file is similar to using git reset with a file path, except it updates the working directory instead of the stage. Unlike the commit-level version of this command, this does not move the HEAD reference, which means that you won’t switch branches.

For more details see the last section of this article.

Just Shadow
  • 10,860
  • 6
  • 57
  • 75
  • I tried that too. The contents do change but when I run `git log`, it seems like the history was not reset at all – doctopus Jul 28 '20 at 06:31
  • @doctopus the history shouldn't be changed, as we changing the contents only of a single file. If you want to reset the history as well, then you'll need to checkout without providing the `filename`. BUT BEWARE, in this case all the files will be resetted to the previous version instead of a single file. – Just Shadow Jul 28 '20 at 07:38
  • 1
    Ahh i see. So head is still pointing to head. Is `git checkout c3e715e filename` equivalent to `git reset c3e715e filename` then? – doctopus Jul 28 '20 at 08:06
  • Not quite. If you've staged that file, then `reset` will unstage it and apply the changes (so you'll completely lose your pending staged changes). But in case of `checkout` it will not touch the staged version, but instead apply the changes as unstaged (just changes the file contents). This is a better option imho as after checkout you can compare the staged and unstaged versions to see if you're okay with the changes. And if so, then stage it as well. – Just Shadow Jul 28 '20 at 08:20
3

git log shows the commits you have made, but neither of those commands creates a new commit. Check the file contents, they should have been changed to the state of commit c3e715e.

Adapted from mkrieger1's comment

wjandrea
  • 28,235
  • 9
  • 60
  • 81