在`read-tree --empty`和`reset --hard`之后恢复丢失的文件

时间:2020-04-09 18:09:15

标签: git

OS:Ubuntu 19.10
git:2.20.1

我只是花费大量时间来编写一些文档。我保存了降价促销 我的文档站点的项目文件夹中的文件为: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")

如您所见,新文档未被跟踪,但是还有很多 修改的文件。这是由于文件夹(^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
-

四处逛逛后,我遇到了this stack overflow answer 这个问题与我当时遇到的问题类似。

$ 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

接受的答案无效,因此我尝试了软重置:

$ 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")

如您所见,这也不起作用。我决定尝试第二个答案中的建议:

$ git help read-tree

$ git read-tree -n --empty

由于空运行没有任何输出,因此我认为继续进行是安全的:

$ 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

您可以看到content / topics / workflow / docker / git-workflow.md 被添加到上面的阶段。但是,在这一点上,我不想在git历史记录中进行如此大规模的删除和添加文件。所以,我决定(愚蠢地)硬重置最后一个 提交。

$ 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

我开始感到恐慌。我的新文档文件发生了什么事?

$ 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

我决定在硬重置之前恢复到头部状态:

$ 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?

但是现在content目录(带有我的新文件)已经消失了。

$ 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 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

我刚刚开始尝试一些绝对行不通的事情:

$ 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
...

所有这些之后,这是最后的参考记录:

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

我不知道如何找回该文件。我什至尝试过:

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

但是,没有结果。

对找到丢失文件的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

借助this answer和以下命令,我能够找到文件:

set_data = set(data.items()) 

[i in set_data for i in desiredValues.items()]
# [True, False, True]