简体   繁体   中英

git bug: `git add` added more files then I expected and `git diff` is missing them after the commit

I just encountered a strange Git behavior and not sure it is a bug or not. I was using Git 2.24 but then I upgraded to 2.26 and the behavior didn't change.

So I had the following setup (only affected files are shown):

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

+-- scripts
    +-- level.gd

So I made my change to level.gd and when deciding to commit noticed that I don't need addons directory. So I decided to get rid of it and issued the following commands:

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

Everything seemed intact: git log shows:

> 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

So I continue with my first idea and when running git status I see there is nothing left to commit? Are my changes gone,. No? a quick check shows they are there: Have they been commited? Seems not:

> 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

What the h?

> 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

Let's annotate the file:

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

Look, my changes are in commit 8be347279 . But I'm 100% sure I didn't stage them there. And diff shows no file changes on that file! But annotate does.

And what's even more strange:

> 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

Why on Earth there is a difference in git log and git diff output and how my changes made into the commit?? Is a Git bug or I have a misunderstanding how Git basically works?

EDIT1: Can't reproduce it. After doing git reset --soft HEAD~1 I got all the changes back and staged:

> 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

So I unstaged them with git reset and properly re-added into 2 commits and now everything worked smooth.

EDIT2: Ok, at least part of the problem is solved: it is indeed a bug in 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> 

But I still don't get how my changes ended up in the commit.

OK, it seems it was my misunderstanding. git diff --summary doesn't show changed files (somehow counter-intuitive), but git diff --compact-summary does. That tricked me. Since it's not a git bug, the second question (why the files were staged) is not that important, I'm closing the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM