在 Git 中,文件怎么可能在 2 次提交之间悄无声息地丢失?
How is it possible in Git that files silently lost between 2 commits?
我和同事在 Git 中遇到过这种情况,两次提交之间文件丢失,但我们没有看到任何 "delete mode 100644 path/to/file" 类型的输出。根据我对 Git 的理解,我不明白这怎么可能。
这里是简化的跟踪信息。让我知道是否有任何进一步的输出有助于调查,我会很乐意追加。
我们在一个分支中找到了 2 个连续的提交:
git log --oneline
这给出:
注意 1423249
和 341c603
。我现在将检查这些提交中的每一个,以显示一个示例文件(还有更多)存在于上一次提交 341c603
中,但在下一次提交中丢失 1423249
.
341c603
➜ git checkout 341c603
Note: checking out '341c603'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 341c603 v0.1.230
➜ git log --stat --summary -- app/components/BreadcrumbNav/index.js
commit f88f71e4bec82a004626d2e1326bb1a1544675b3
Author: panguoliang <panguoliang@dae.org>
Date: Wed May 8 13:31:05 2019 +0800
ADD fiat module
app/components/BreadcrumbNav/index.js | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 app/components/BreadcrumbNav/index.js
➜ ls -l app/components/BreadcrumbNav/index.js
-rw-r--r-- 1 wang.boyang staff 888 May 10 10:10 app/components/BreadcrumbNav/index.js
它在 341c603
中。它是在较旧的提交中创建的。现在搬到 1423249
1423249
➜ git checkout 1423249
Previous HEAD position was 341c603 v0.1.230
HEAD is now at 1423249 fix websocket, subscribe to current symbol
➜ git log --stat --summary -- ./app/components/BreadcrumbNav/index.js
(empty output)
➜ ls -l app/components/BreadcrumbNav/index.js
ls: app/components/BreadcrumbNav/index.js: No such file or directory
没有!那么,1423249
有哪些变化呢?丢失的文件 app/components/BreadcrumbNav/index.js
是否已在 1423249
中删除?
➜ git show --stat 1423249
commit 1423249134941968692d2094c64369017838d393 (HEAD)
Author: Wang Boyang <boyang@dae.org>
Date: Thu May 9 12:59:59 2019 +0800
fix websocket, subscribe to current symbol
.eslintrc.js | 2 ++
.prettierrc | 14 ++++++---
app/containers/BarBox/actions.js | 24 ++++++--------
app/containers/BarBox/constants.js | 8 ++---
app/containers/BarBox/index.js | 22 ++++++-------
app/containers/BarBox/reducer.js | 18 ++++-------
app/containers/BarBox/saga.js | 20 ++++++------
app/containers/BarBox/selectors.js | 5 ++-
app/containers/BarBox/styles.js | 5 ++-
app/containers/TradePage/index.js | 116 ++++++++++++++++++++++++++++----------------------------------------
10 files changed, 102 insertions(+), 132 deletions(-)
绝对没有与丢失文件相关的更改,app/components/BreadcrumbNav/index.js
。那么,发生了什么?
编辑
下面是 git log --graph
的简化输出:
* commit 47750dc0059d503dcd73de767fe9a1914070b6b9
|\ Merge: d1a0f6e 2115249
| | Author: justinji <justin5562587@gmail.com>
| | Date: Thu May 9 17:40:01 2019 +0800
| |
| | resolve conflicts
| |
| * commit 2d8835e30a9a27f9eec687de573e4a77e66ed69a
| |\ Merge: 324857a 341c603
| | | Author: Wang Boyang <boyang@dae.org>
| | | Date: Thu May 9 13:06:22 2019 +0800
| | |
| | | Merge remote-tracking branch 'origin/test' into test
| | |
| | * commit 341c60334919930ee5f731cae3660c80aefb2ca1
| | | Author: panguoliang <panguoliang@dae.org>
| | | Date: Thu May 9 12:03:01 2019 +0800
| | |
| | | v0.1.230
| | |
| | |
| | * commit 66eaf470248a01b6d7861f1b319335255363195c
| | |\ Merge: 5ec1650 63e528b
| | | | Author: panguoliang <panguoliang@dae.org>
| | | | Date: Thu May 9 12:00:48 2019 +0800
| | | |
| | | | lint
| | | |
| | * | commit 9f8b93645851d98b1ab08264de8700a0e0dff47c
| | | | Author: panguoliang <panguoliang@dae.org>
| | | | Date: Thu May 9 11:58:41 2019 +0800
| | | |
| | | | ADD Fiat deposit
| | | |
| | | |
| * | | commit 1423249134941968692d2094c64369017838d393
| | | | Author: Wang Boyang <boyang@dae.org>
| | | | Date: Thu May 9 12:59:59 2019 +0800
| | | |
| | | | fix websocket, subscribe to current symbol
| | | |
| * | | commit 6d2c1073689140b75758d0e008af183340f1842b
| |\ \ \ Merge: f730698 63e528b
| | | |/ Author: Wang Boyang <boyang@dae.org>
| | |/| Date: Thu May 9 10:39:23 2019 +0800
| | | |
| | | | Merge remote-tracking branch 'origin/test' into test
| | | |
调查结果:在合并提交中(及之后)2d8835e30a9a27f9eec687de573e4a77e66ed69a
,文件丢失。 cat 文件输出:
➜ git cat-file commit 2d8835e30a9a27f9eec687de573e4a77e66ed69a
tree a46f997a76ff403d1ec0567913722787d72bf9f6
parent 324857aa9a26852a2c5699650366ac7797a85ac2
parent 341c60334919930ee5f731cae3660c80aefb2ca1
author Wang Boyang <boyang@dae.org> 1557378382 +0800
committer Wang Boyang <boyang@dae.org> 1557378382 +0800
Merge remote-tracking branch 'origin/test' into test
编辑2
工具 difflame 给出以下输出:
➜ ~/Projects/difflame.py 341c603 2d8835e30a9a27f9eec687de573e4a77e66ed69a -- app/components/BreadcrumbNav/index.js
Processing line 48/48
diff --git a/app/components/BreadcrumbNav/index.js b/app/components/BreadcrumbNav/index.js
deleted file mode 100644
index bf18d9d..0000000
--- a/app/components/BreadcrumbNav/index.js
+++ /dev/null
@@ -1,41 +0,0 @@
341c603: v0.1.230
-341c603 (panguoliang 2019-05-09 12:03:01 1 ) /**
-341c603 (panguoliang 2019-05-09 12:03:01 2 ) *
-341c603 (panguoliang 2019-05-09 12:03:01 3 ) * BreadcrumbNav
-341c603 (panguoliang 2019-05-09 12:03:01 4 ) *
-341c603 (panguoliang 2019-05-09 12:03:01 5 ) */
有什么方法可以找出为什么合并是这样完成的(即删除文件),以便将来避免这种情况?
如果修订一个接一个(看不到....它们可能在两个不同的分支上,因为您没有指定 --graph)然后 git show --name-status 1423249
应该显示文件已删除。
PS 鉴于修订实际上在两个独立的分支上,这意味着 可能 删除可能发生在两个修订的任何祖先上。 .. 或者没有涉及真正的删除。这取决于 2 个分支的历史记录(共同祖先可能缺少两个文件,一个分支仍然缺少它们,它们被添加到另一个分支上......在这种情况下不涉及删除)。
所有评论和回答都非常有用。我终于找到原因了。
首先,就像@meagar 提到的那样,文件删除只是由不在提交快照中的文件 blob 表示。事实证明,文件在合并过程中消失了。
使用任何理智的 mergetool,当遇到 2 parents 其中一个添加文件而另一个没有添加文件时,它会提示并可能添加文件,或显示冲突,如 modified in XXXX but deleted in XXX
。
这个没有。因为我弄错了下面2个:
git merge -s recursive -X ours
git merge -s ours
后者会简单地忽略另一个 parents。因此,那里的变化消失了。从那以后,一切都没有按预期工作,文件删除被错误地保留
我和同事在 Git 中遇到过这种情况,两次提交之间文件丢失,但我们没有看到任何 "delete mode 100644 path/to/file" 类型的输出。根据我对 Git 的理解,我不明白这怎么可能。
这里是简化的跟踪信息。让我知道是否有任何进一步的输出有助于调查,我会很乐意追加。
我们在一个分支中找到了 2 个连续的提交:
git log --oneline
这给出:
注意 1423249
和 341c603
。我现在将检查这些提交中的每一个,以显示一个示例文件(还有更多)存在于上一次提交 341c603
中,但在下一次提交中丢失 1423249
.
341c603
➜ git checkout 341c603
Note: checking out '341c603'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 341c603 v0.1.230
➜ git log --stat --summary -- app/components/BreadcrumbNav/index.js
commit f88f71e4bec82a004626d2e1326bb1a1544675b3
Author: panguoliang <panguoliang@dae.org>
Date: Wed May 8 13:31:05 2019 +0800
ADD fiat module
app/components/BreadcrumbNav/index.js | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 app/components/BreadcrumbNav/index.js
➜ ls -l app/components/BreadcrumbNav/index.js
-rw-r--r-- 1 wang.boyang staff 888 May 10 10:10 app/components/BreadcrumbNav/index.js
它在 341c603
中。它是在较旧的提交中创建的。现在搬到 1423249
1423249
➜ git checkout 1423249
Previous HEAD position was 341c603 v0.1.230
HEAD is now at 1423249 fix websocket, subscribe to current symbol
➜ git log --stat --summary -- ./app/components/BreadcrumbNav/index.js
(empty output)
➜ ls -l app/components/BreadcrumbNav/index.js
ls: app/components/BreadcrumbNav/index.js: No such file or directory
没有!那么,1423249
有哪些变化呢?丢失的文件 app/components/BreadcrumbNav/index.js
是否已在 1423249
中删除?
➜ git show --stat 1423249
commit 1423249134941968692d2094c64369017838d393 (HEAD)
Author: Wang Boyang <boyang@dae.org>
Date: Thu May 9 12:59:59 2019 +0800
fix websocket, subscribe to current symbol
.eslintrc.js | 2 ++
.prettierrc | 14 ++++++---
app/containers/BarBox/actions.js | 24 ++++++--------
app/containers/BarBox/constants.js | 8 ++---
app/containers/BarBox/index.js | 22 ++++++-------
app/containers/BarBox/reducer.js | 18 ++++-------
app/containers/BarBox/saga.js | 20 ++++++------
app/containers/BarBox/selectors.js | 5 ++-
app/containers/BarBox/styles.js | 5 ++-
app/containers/TradePage/index.js | 116 ++++++++++++++++++++++++++++----------------------------------------
10 files changed, 102 insertions(+), 132 deletions(-)
绝对没有与丢失文件相关的更改,app/components/BreadcrumbNav/index.js
。那么,发生了什么?
编辑
下面是 git log --graph
的简化输出:
* commit 47750dc0059d503dcd73de767fe9a1914070b6b9
|\ Merge: d1a0f6e 2115249
| | Author: justinji <justin5562587@gmail.com>
| | Date: Thu May 9 17:40:01 2019 +0800
| |
| | resolve conflicts
| |
| * commit 2d8835e30a9a27f9eec687de573e4a77e66ed69a
| |\ Merge: 324857a 341c603
| | | Author: Wang Boyang <boyang@dae.org>
| | | Date: Thu May 9 13:06:22 2019 +0800
| | |
| | | Merge remote-tracking branch 'origin/test' into test
| | |
| | * commit 341c60334919930ee5f731cae3660c80aefb2ca1
| | | Author: panguoliang <panguoliang@dae.org>
| | | Date: Thu May 9 12:03:01 2019 +0800
| | |
| | | v0.1.230
| | |
| | |
| | * commit 66eaf470248a01b6d7861f1b319335255363195c
| | |\ Merge: 5ec1650 63e528b
| | | | Author: panguoliang <panguoliang@dae.org>
| | | | Date: Thu May 9 12:00:48 2019 +0800
| | | |
| | | | lint
| | | |
| | * | commit 9f8b93645851d98b1ab08264de8700a0e0dff47c
| | | | Author: panguoliang <panguoliang@dae.org>
| | | | Date: Thu May 9 11:58:41 2019 +0800
| | | |
| | | | ADD Fiat deposit
| | | |
| | | |
| * | | commit 1423249134941968692d2094c64369017838d393
| | | | Author: Wang Boyang <boyang@dae.org>
| | | | Date: Thu May 9 12:59:59 2019 +0800
| | | |
| | | | fix websocket, subscribe to current symbol
| | | |
| * | | commit 6d2c1073689140b75758d0e008af183340f1842b
| |\ \ \ Merge: f730698 63e528b
| | | |/ Author: Wang Boyang <boyang@dae.org>
| | |/| Date: Thu May 9 10:39:23 2019 +0800
| | | |
| | | | Merge remote-tracking branch 'origin/test' into test
| | | |
调查结果:在合并提交中(及之后)2d8835e30a9a27f9eec687de573e4a77e66ed69a
,文件丢失。 cat 文件输出:
➜ git cat-file commit 2d8835e30a9a27f9eec687de573e4a77e66ed69a
tree a46f997a76ff403d1ec0567913722787d72bf9f6
parent 324857aa9a26852a2c5699650366ac7797a85ac2
parent 341c60334919930ee5f731cae3660c80aefb2ca1
author Wang Boyang <boyang@dae.org> 1557378382 +0800
committer Wang Boyang <boyang@dae.org> 1557378382 +0800
Merge remote-tracking branch 'origin/test' into test
编辑2
工具 difflame 给出以下输出:
➜ ~/Projects/difflame.py 341c603 2d8835e30a9a27f9eec687de573e4a77e66ed69a -- app/components/BreadcrumbNav/index.js
Processing line 48/48
diff --git a/app/components/BreadcrumbNav/index.js b/app/components/BreadcrumbNav/index.js
deleted file mode 100644
index bf18d9d..0000000
--- a/app/components/BreadcrumbNav/index.js
+++ /dev/null
@@ -1,41 +0,0 @@
341c603: v0.1.230
-341c603 (panguoliang 2019-05-09 12:03:01 1 ) /**
-341c603 (panguoliang 2019-05-09 12:03:01 2 ) *
-341c603 (panguoliang 2019-05-09 12:03:01 3 ) * BreadcrumbNav
-341c603 (panguoliang 2019-05-09 12:03:01 4 ) *
-341c603 (panguoliang 2019-05-09 12:03:01 5 ) */
有什么方法可以找出为什么合并是这样完成的(即删除文件),以便将来避免这种情况?
如果修订一个接一个(看不到....它们可能在两个不同的分支上,因为您没有指定 --graph)然后 git show --name-status 1423249
应该显示文件已删除。
PS 鉴于修订实际上在两个独立的分支上,这意味着 可能 删除可能发生在两个修订的任何祖先上。 .. 或者没有涉及真正的删除。这取决于 2 个分支的历史记录(共同祖先可能缺少两个文件,一个分支仍然缺少它们,它们被添加到另一个分支上......在这种情况下不涉及删除)。
所有评论和回答都非常有用。我终于找到原因了。
首先,就像@meagar 提到的那样,文件删除只是由不在提交快照中的文件 blob 表示。事实证明,文件在合并过程中消失了。
使用任何理智的 mergetool,当遇到 2 parents 其中一个添加文件而另一个没有添加文件时,它会提示并可能添加文件,或显示冲突,如 modified in XXXX but deleted in XXX
。
这个没有。因为我弄错了下面2个:
git merge -s recursive -X ours
git merge -s ours
后者会简单地忽略另一个 parents。因此,那里的变化消失了。从那以后,一切都没有按预期工作,文件删除被错误地保留