Discussion

A surprising way to lose your file in Git

In the spirit of the recent: https://news.ycombinator.com/item?id=33047150 here is how git can eat your file:

We make a new directory, initialize a Git repo there:

  $ mkdir git-test
  $ cd git-test
  git-test$ git init
  Initialized empty Git repository in /home/kaz/git-test/.git/
Now we create a file and stage it for addition:

  git-test$ echo content > file
  git-test$ git add file
We can see it in the dir listing, of course:

  git-test$ ls
  file
And see the diff between the stage and the index, which adds the file:

  git-test$ git diff --cached
  diff --git a/file b/file
  new file mode 100644
  index 0000000..d95f3ad
  --- /dev/null
  +++ b/file
  @@ -0,0 +1 @@
  +content
Now, we do a git reset --hard to eliminate the staging:

  git-test $ git reset --hard
Where is the working file?

  git-test $ ls
  git-test $ 
Oops!
7 points kazinator 4 years ago 31 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton