在"读取树--空"和"重置--hard"之后恢复丢失的文件



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
-

环顾四周后,我遇到了这个堆栈溢出答案 这个问题与我当时的问题相似。

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

但可惜,没有结果。

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

由于这个答案和以下命令,我能够找到该文件:

$ git fsck --cache --no-reflogs --lost-found --dangling HEAD
$ cd .git/lost-found/other
$ cat ./*

最新更新