git bug:`git add`添加了更多文件,然后我期望,并且`git diff`在提交后丢失了它们

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git bug:`git add`添加了更多文件,然后我期望,并且`git diff`在提交后丢失了它们相关的知识,希望对你有一定的参考价值。

我刚刚遇到了奇怪的Git行为,不确定是否是错误。我使用的是Git 2.24,但后来升级到2.26,行为没有改变。

因此,我进行了以下设置(仅显示受影响的文件:]

.
+-- addons
    +-- gut
        +-- file1
        +-- file2

+-- scripts
    +-- level.gd

因此,我对level.gd进行了更改,并且在决定提交时注意到我不需要addons目录。因此,我决定摆脱它,并发出以下命令:

rm -Rf addons
git add addons/
git commit -m "removed addons/gut"

一切似乎都完好无缺:git log显示:

> git log
commit 8be3472798df93d015ab3023f9e9d406a76dbed5 (HEAD -> master)
Author: hedin_hiervard <flame.beholder@gmail.com>
Date:   Sun Apr 26 11:13:02 2020 +0200

    removed addons/gut

所以我继续我的第一个想法,当运行git status时,我看到没有什么可提交的了!我的更改不见了吗?不,快速检查显示它们在那里。他们被提交了吗?似乎不是:

> git diff --summary -r HEAD^1 -r HEAD

delete mode 100644 addons/gut/GutScene.gd
 delete mode 100644 addons/gut/GutScene.tscn
 delete mode 100644 addons/gut/LICENSE.md
 delete mode 100644 addons/gut/doubler.gd
 delete mode 100644 addons/gut/gut.gd
 delete mode 100644 addons/gut/gut_cmdln.gd
 delete mode 100644 addons/gut/gut_plugin.gd
 delete mode 100644 addons/gut/hook_script.gd
 delete mode 100644 addons/gut/icon.png
 delete mode 100644 addons/gut/icon.png.import
 delete mode 100644 addons/gut/logger.gd
 delete mode 100644 addons/gut/method_maker.gd
 delete mode 100644 addons/gut/one_to_many.gd
 delete mode 100644 addons/gut/optparse.gd
 delete mode 100644 addons/gut/plugin.cfg
 delete mode 100644 addons/gut/signal_watcher.gd
 delete mode 100644 addons/gut/source_code_pro.fnt
 delete mode 100644 addons/gut/spy.gd
 delete mode 100644 addons/gut/stub_params.gd
 delete mode 100644 addons/gut/stubber.gd
 delete mode 100644 addons/gut/summary.gd
 delete mode 100644 addons/gut/test.gd
 delete mode 100644 addons/gut/test_collector.gd
 delete mode 100644 addons/gut/thing_counter.gd
 delete mode 100644 addons/gut/utils.gd

什么?

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

nothing to commit, working tree clean

让我们注释文件:

> git annotate scripts/level.gd

...
8be347279   (hedin_hiervard 2020-04-26 11:13:02 +0200   2708)   var weather_probs_array = rules.all_rules_in_section("weather_prob")
...

看,我的更改在提交8be347279中!但是我100%确信我没有在那儿演出。而diff显示该文件没有文件更改!但是注释确实如此。

还有更奇怪的是:

> git log --name-status HEAD^..HEAD
commit 8be3472798df93d015ab3023f9e9d406a76dbed5 (HEAD -> master)
Author: hedin_hiervard <flame.beholder@gmail.com>
Date:   Sun Apr 26 11:13:02 2020 +0200

    removed addons/gut

D       addons/gut/GutScene.gd
D       addons/gut/GutScene.tscn
D       addons/gut/LICENSE.md
D       addons/gut/doubler.gd
D       addons/gut/gut.gd
D       addons/gut/gut_cmdln.gd
D       addons/gut/gut_plugin.gd
D       addons/gut/hook_script.gd
D       addons/gut/icon.png
D       addons/gut/icon.png.import
D       addons/gut/logger.gd
D       addons/gut/method_maker.gd
D       addons/gut/one_to_many.gd
D       addons/gut/optparse.gd
D       addons/gut/plugin.cfg
D       addons/gut/signal_watcher.gd
D       addons/gut/source_code_pro.fnt
D       addons/gut/spy.gd
D       addons/gut/stub_params.gd
D       addons/gut/stubber.gd
D       addons/gut/summary.gd
D       addons/gut/test.gd
D       addons/gut/test_collector.gd
D       addons/gut/thing_counter.gd
D       addons/gut/utils.gd
M       rules/level.cfg
M       scripts/level.gd

为什么在地球上git loggit diff输出不同,以及我如何更改提交?是Git错误还是我对Git的基本工作方式有误解?

EDIT1:无法复制。完成git reset --soft HEAD~1之后,我找回了所有更改并上演了:

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

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    addons/gut/GutScene.gd
    deleted:    addons/gut/GutScene.tscn
    deleted:    addons/gut/LICENSE.md
    deleted:    addons/gut/doubler.gd
    deleted:    addons/gut/gut.gd
    deleted:    addons/gut/gut_cmdln.gd
    deleted:    addons/gut/gut_plugin.gd
    deleted:    addons/gut/hook_script.gd
    deleted:    addons/gut/icon.png
    deleted:    addons/gut/icon.png.import
    deleted:    addons/gut/logger.gd
    deleted:    addons/gut/method_maker.gd
    deleted:    addons/gut/one_to_many.gd
    deleted:    addons/gut/optparse.gd
    deleted:    addons/gut/plugin.cfg
    deleted:    addons/gut/signal_watcher.gd
    deleted:    addons/gut/source_code_pro.fnt
    deleted:    addons/gut/spy.gd
    deleted:    addons/gut/stub_params.gd
    deleted:    addons/gut/stubber.gd
    deleted:    addons/gut/summary.gd
    deleted:    addons/gut/test.gd
    deleted:    addons/gut/test_collector.gd
    deleted:    addons/gut/thing_counter.gd
    deleted:    addons/gut/utils.gd
    modified:   rules/level.cfg
    modified:   scripts/level.gd

所以我用git reset将其暂存,并正确地重新添加到2个commit中,现在一切正常。

EDIT2:好的,至少部分问题得以解决:这确实是git diff --summary中的错误:

hedin@MacBook-Pro ~/P/ds> git diff -r HEAD^1 -r HEAD
diff --git a/scripts/board.gd b/scripts/board.gd
index a9f177a0..43eeae10 100644
--- a/scripts/board.gd
+++ b/scripts/board.gd
@@ -2927,11 +2927,6 @@ func set_observatory_notices() -> void:
                if b.is_observatory():
                        b.observatory_set_new_cards(true)

-func reset_all_workshops() -> void:
-       for b in _tower_blocks.values():
-               if b.is_workshop():
-                       b.reset_timer(Block.TimerType.WORKSHOP_PRODUCING)
-
 func has_charged_dynamite() -> bool:
        for b in _blocks.values():
                if b.has_charged_dynamite():
diff --git a/scripts/level.gd b/scripts/level.gd
index cfa1f0df..d079cedb 100644
--- a/scripts/level.gd
+++ b/scripts/level.gd
@@ -305,10 +305,7 @@ func _on_player_decks_can_draw_changed(type: int) -> void:
        if player_decks.decks[type].can_draw():
                return
        _update_ui_state()
-       match type:
-               CardDesc.Type.TOOL:
-                       board.reset_all_workshops()
-
+       
 func _set_player_draws_left(val):
        player_draws_left = val
        emit_signal("player_draws_changed", val)
hedin@MacBook-Pro ~/P/ds> git diff --summary -r HEAD^1 -r HEAD
hedin@MacBook-Pro ~/P/ds> git diff --compact-summary -r HEAD^1 -r HEAD
 scripts/board.gd | 5 -----
 scripts/level.gd | 5 +----
 2 files changed, 1 insertion(+), 9 deletions(-)
hedin@MacBook-Pro ~/P/ds> 

但是我仍然不知道我的更改最终如何提交。

答案

好,看来这是我的误会。 git diff --summary不显示更改的文件(某种程度上是违反直觉的),但是git diff --compact-summary可以显示。那欺骗了我。由于它不是一个git bug,因此第二个问题(为什么要暂存文件)不是那么重要,我要结束这个问题。

以上是关于git bug:`git add`添加了更多文件,然后我期望,并且`git diff`在提交后丢失了它们的主要内容,如果未能解决你的问题,请参考以下文章

git add 回退 和 git commit 回退

git add 回退 和 git commit 回退

git add 回退 和 git commit 回退

git add 添加错文件的撤销方法

[Git] Use git add --patch for better commit history and mitigating bugs

git篇之add