0

OS: Ubuntu 19.10
git: 2.20.1

I just spend a lot of time writing up some documentation. I saved the markdown file in my documentation site's project folder as: content/topics/workflow/docker/git-workflow.md

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .gitignore
        modified:   .prettierignore
        ...
        modified:   src/templates/topic/pages.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        content/topics/workflow/docker/git-workflow.md

no changes added to commit (use "git add" and/or "git commit -a")

As you can see, the new document is untracked, but there are also a ton of modified files. This was due to an issue with line endings in the folder (^M):

$ git diff
diff --git a/.gitignore b/.gitignore
index dcf9f37..f1a3279 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
-.env
-dist
-tmp
-node_modules
-.vscode
+.env^M
+dist^M
+tmp^M
+node_modules^M
+.vscode^M
diff --git a/.prettierignore b/.prettierignore
index d282fbc..81f839a 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -1,4 +1,4 @@
-src/templates/**/header.html
-src/templates/**/footer.html
-nav.html
+src/templates/**/header.html^M
+src/templates/**/footer.html^M
+nav.html^M
 pages.html
\ No newline at end of file
diff --git a/content/topics/aws/dbaccess/authorize-ip-in-aws.md b/content/topics/aws/dbaccess/authorize-ip-in-aws.md
index a37f9ee..7ffb9dc 100644
--- a/content/topics/aws/dbaccess/authorize-ip-in-aws.md
+++ b/content/topics/aws/dbaccess/authorize-ip-in-aws.md
@@ -1,50 +1,50 @@
-## Step 1
-

After looking around for a bit, I came across this stack overflow answer to a question that was similar to the one I had at the time.

$ git add --renormalize .

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitignore
        ...
        modified:   src/templates/topic/pages.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        content/topics/workflow/docker/git-workflow.md

The accepted answer did not work, so I tried a soft reset:

$ git reset HEAD -- .
Unstaged changes after reset:
M       .gitignore
...
M       content/topics/workflow/1overview/starting-with-a-fresh-repo.md
M       content/topics/workflow/docker/setup.md
M       content/topics/workflow/merging/meta.json
...
M       src/templates/topic/pages.html

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   .gitignore
        ...
        modified:   src/templates/topic/pages.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        content/topics/workflow/docker/git-workflow.md

no changes added to commit (use "git add" and/or "git commit -a")

As you can see, this also did not work. I decided to try the suggestions in the second answer:

$ git help read-tree

$ git read-tree -n --empty

As there was no output from the dry run, I decided it was safe to proceed:

$ git read-tree --empty

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    .gitignore
        ...
        deleted:    content/topics/workflow/1overview/what-next.md
        deleted:    content/topics/workflow/docker/installation.md
        deleted:    content/topics/workflow/docker/meta.json
        deleted:    content/topics/workflow/docker/setup-docker-on-windows-home.md
        deleted:    content/topics/workflow/docker/setup.md
        deleted:    content/topics/workflow/docker/usage.md
        deleted:    content/topics/workflow/merging/meta.json
        ...
        deleted:    src/templates/topic/pages.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        .netlify/
        .prettierignore
        README.md
        content/
        gulpfile.js
        index.js
        netlify.toml
        package-lock.json
        package.json
        src/


$ git commit -m 'fix line endings?'
[master f65c8e2] fix line endings?
 111 files changed, 8013 deletions(-)
 delete mode 100644 .gitignore
 ...
 delete mode 100644 content/topics/workflow/1overview/what-next.md
 delete mode 100644 content/topics/workflow/docker/installation.md
 delete mode 100644 content/topics/workflow/docker/meta.json
 delete mode 100644 content/topics/workflow/docker/setup-docker-on-windows-home.md
 delete mode 100644 content/topics/workflow/docker/setup.md
 delete mode 100644 content/topics/workflow/docker/usage.md
 delete mode 100644 content/topics/workflow/merging/meta.json
 ...
 delete mode 100644 src/templates/topic/pages.html

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        .netlify/
        .prettierignore
        README.md
        content/
        gulpfile.js
        index.js
        netlify.toml
        package-lock.json
        package.json
        src/

nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   .gitignore
        ...
        new file:   content/topics/workflow/docker/git-workflow.md
        ...
        new file:   src/templates/topic/pages.html

You can see that content/topics/workflow/docker/git-workflow.md was added to staging above. At this point, though, I didn't want to have this massive deletion and addition of files in my git history. So, I decided (stupidly) to hard reset the last commit.

$ git reset --hard HEAD^
HEAD is now at a6daf28 add .env.php note

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

I began to panic. What happened to my new documentation file?

$ graph
* a6daf28 (HEAD -> master, origin/master) add .env.php note
* 08845e9 add note about .txt for db_root_password
* c7ba3ab fix typos, add windows 10 home warning
...
* 3d19355 adding production repo setup documentation

I decided to revert back to the head state before the hard reset:

$ git reflog
a6daf28 (HEAD -> master, origin/master) HEAD@{0}: reset: moving to HEAD^
f65c8e2 HEAD@{1}: commit: fix line endings?
a6daf28 (HEAD -> master, origin/master) HEAD@{2}: commit: add .env.php note
08845e9 HEAD@{3}: commit: add note about .txt for db_root_password
c7ba3ab HEAD@{4}: commit: fix typos, add windows 10 home warning
...

$ git reset --hard f65c8e2
HEAD is now at f65c8e2 fix line endings?

But now, the content directory (with my new file) is gone.

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .vscode/
        dist/
        node_modules/

nothing added to commit but untracked files present (use "git add" to track)

Here, I really started to panic. I decided (again, stuipdly) to hard reset AGAIN.

$ git reset --hard HEAD^
HEAD is now at a6daf28 add .env.php note

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

I just started trying random things that were definitely not going to work:

$ git pull
Warning: Permanently added the RSA host key for IP address 'REDACTED' to the list of known hosts.
Already up to date.

$ git push
Everything up-to-date

$ git reset --hard HEAD^
HEAD is now at 08845e9 add note about .txt for db_root_password
$ git reflog
08845e9 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
a6daf28 (origin/master) HEAD@{1}: reset: moving to HEAD^
f65c8e2 HEAD@{2}: reset: moving to f65c8e2
a6daf28 (origin/master) HEAD@{3}: reset: moving to HEAD^
f65c8e2 HEAD@{4}: commit: fix line endings?
a6daf28 (origin/master) HEAD@{5}: commit: add .env.php note
08845e9 (HEAD -> master) HEAD@{6}: commit: add note about .txt for db_root_password
c7ba3ab HEAD@{7}: commit: fix typos, add windows 10 home warning
...
$ git reset --hard f65c
HEAD is now at f65c8e2 fix line endings?
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .vscode/
        dist/
        node_modules/

nothing added to commit but untracked files present (use "git add" to track)
$ git ls-files -d | sed -e "s/\(.*\)/'\1'/" | xargs git checkout --
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .vscode/
        dist/
        node_modules/

nothing added to commit but untracked files present (use "git add" to track)
$ graph
* f65c8e2 (HEAD -> master) fix line endings?
* a6daf28 (origin/master) add .env.php note
* 08845e9 add note about .txt for db_root_password
* c7ba3ab fix typos, add windows 10 home warning
...

After all of this, here is the final reflog:

f65c8e2 (HEAD -> master) HEAD@{0}: reset: moving to f65c
08845e9 HEAD@{1}: reset: moving to HEAD^
a6daf28 (origin/master) HEAD@{2}: reset: moving to HEAD^
f65c8e2 (HEAD -> master) HEAD@{3}: reset: moving to f65c8e2
a6daf28 (origin/master) HEAD@{4}: reset: moving to HEAD^
f65c8e2 (HEAD -> master) HEAD@{5}: commit: fix line endings?
a6daf28 (origin/master) HEAD@{6}: commit: add .env.php note
08845e9 HEAD@{7}: commit: add note about .txt for db_root_password
c7ba3ab HEAD@{8}: commit: fix typos, add windows 10 home warning
8fd9f35 HEAD@{9}: commit: update setup documentation
83ca65d HEAD@{10}: commit: docker setup documentation complete
8305494 HEAD@{11}: commit: not sure where that file came from
6758e4f HEAD@{12}: commit: added docker workflow
18a5ec2 HEAD@{13}: commit: add git gc
2ed635d HEAD@{14}: pull: Fast-forward
7f02337 HEAD@{15}: commit: updated phpmyadmin docs and git docs
3f55c45 HEAD@{16}: commit: updated docs with lastpass data
7c7dfbb HEAD@{17}: commit: updated docs with lastpass data
cee8bc6 HEAD@{18}: commit: fixed typo
10daf0f HEAD@{19}: commit: added db access documentation
c579c0e HEAD@{20}: commit: fixed bulleting
27dc0c0 HEAD@{21}: commit: added security audit process docs
8ed0326 HEAD@{22}: commit: add migration documentation
ae44fb5 HEAD@{23}: pull: Merge made by the 'recursive' strategy.
2b2ec9c HEAD@{24}: commit: not sure

I cannot figure out how to get this file back. I've even tried:

$ sudo find / -name "git-workflow.md"

But alas, there were no results.

Any help locating the missing file would be greatly appreciated.

davidmwhynot
  • 386
  • 2
  • 10
  • 1
    `git reset HEAD -- .` is actually a *mixed* reset, not a soft one. Still, the trick for finding a dangling blob is the way to go, so the rest of this (and specifically your own answer) is correct. – torek Apr 10 '20 at 00:25

1 Answers1

0

I was able to locate the file thanks to this answer and the following commands:

$ git fsck --cache --no-reflogs --lost-found --dangling HEAD
$ cd .git/lost-found/other
$ cat ./*
davidmwhynot
  • 386
  • 2
  • 10