git commit 在索引中留下更改

标签 git

问题

我正在尝试进行一些更改,但它们被抛在了后面,因为 我不明白的原因:

我看一下当前的 repo 状态:

% git status
On branch new-master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   __init__.py
    new file:   api/__init__.py
    new file:   api/api.py
    new file:   api/common.py
    new file:   api/error.py

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:   ../status/server.py
    modified:   __init__.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    ../env/
    ../foo.db

乱七八糟的,但是索引里的就是我要提交的。 (注意有 未上演的更改:我有兴趣进行这些更改。这 “要提交的更改”下的更改是我希望进行的更改 promise 。)我 promise :

% git commit -m "Why won't you commit?"
[new-master a4dbe36] Why won't you commit?

并立即重新运行状态,以见证没有任何变化:

% git status
On branch new-master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   __init__.py
    new file:   api/__init__.py
    new file:   api/api.py
    new file:   api/common.py
    new file:   api/error.py

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:   ../status/server.py
    modified:   __init__.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    ../env/
    ../foo.db

如果我查看最后一次提交,我们可以看到没有任何内容:

% git log -n1 -p
commit a4dbe36e77a9f3b04c2ef33de75e28b6aa7cac0c
Author: Me <john.smith@example.com>
Date:   Fri May 2 11:52:06 2014 -0700

    Why won't you commit?

这是怎么回事? 为什么没有提交这些更改?

一个最小的测试用例!

mkdir bar && cd bar
git init
echo "data." > some_existing_file
git add some_existing_file
git commit -m "Initial commit."

刚刚设置好了。现在是真正的乐趣。我得到时髦的行为 --intent-to-add (我可能只是不明白它的作用),但这会重现 我的全部问题:

mkdir subdir && mkdir subdir/another
cd subdir
echo "foodata" > a_file
echo "more data" > another/other_data
git add --intent-to-add a_file
git add another/other_data

然后尝试提交:

git commit -m "Why isn't this committing anything?"

可能使用输出

我没有任何自定义 Hook (据我所知):

% ls "`git rev-parse --git-dir`"/hooks
applypatch-msg.sample     pre-applypatch.sample     pre-rebase.sample
commit-msg.sample         pre-commit.sample         prepare-commit-msg.sample
post-update.sample        pre-push.sample           update.sample

如果重要的话,添加文件所在的目录也是新的,并且确实 不存在于先前的提交中。 (当我运行时,我在目录本身 git status,请参阅 not-staged-for-commit 部分中的 ..。)

git diff HEAD a4dbe36e77a,按照要求,不输出任何东西。

git 显示:

% git show a4dbe36e77a
commit a4dbe36e77a9f3b04c2ef33de75e28b6aa7cac0c
Author: Me <john.smith@example.com>
Date:   Fri May 2 11:52:06 2014 -0700

    Why won't you commit?

同样,原始的:

% git show --format=raw a4dbe36e77a
commit a4dbe36e77a9f3b04c2ef33de75e28b6aa7cac0c
tree ca9326ccba91dda7540198c082db6e6ab3fb097a
parent 1842d56d5a47ff33bd420a5014f208c85acc5a1f
author Me <john.smith@example.com> 1399056726 -0700
committer Me <john.smith@example.com> 1399056726 -0700

    Why won't you commit?

那棵树的ls-tree:

% git ls-tree --full-tree ca9326ccba91dda7540198c082db6e6ab3fb097a
100644 blob fab05d167a3f8ef9ddf00d7af09e00eea03f1d28    requirements.txt
040000 tree 0d9742d9c1728495d600bde05a7f276f3c65d96d    status

为简洁起见:git diff --staged 显示了变化的差异:这是我希望提交的差异,但是 git commit 没有提交. (git diff --cached 也显示此差异。)git commit -v 也显示此差异,但键入消息不会导致提交。

这个分支唯一真正奇怪的地方在于它是一个孤立的分支——master 包含一个 WIP,但是这个代码的困惑版本。我希望清理它,但保留 master 因为我是人,担心我会删除一些东西。然而,这不是第一次提交。 (第一次提交也有实际更改。)只是现在出了点问题。

潜在线索/此处不对:

编辑 @torek 让我运行这个:

% git write-tree
ca9326ccba91dda7540198c082db6e6ab3fb097a

这应该将索引的内容变成一棵树(然后可以将其附加到提交对象,因此提交就诞生了)。但是,该散列与 HEAD 相同。鉴于我的索引发生了变化,人们会期望树 ID 必须不同。具体来说,这些命令显示出一些奇怪的东西:

% git write-tree
ca9326ccba91dda7540198c082db6e6ab3fb097a
% git diff --staged --stat
 wsgi_util/__init__.py     |  0
 wsgi_util/api/__init__.py |  0
 wsgi_util/api/api.py      | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 wsgi_util/api/common.py   | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 wsgi_util/api/error.py    | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 235 insertions(+)
% git write-tree
ca9326ccba91dda7540198c082db6e6ab3fb097a
% git show --format=raw HEAD
commit fe28d60c21e998104e5967faf7af3bf203cd4b26
tree ca9326ccba91dda7540198c082db6e6ab3fb097a
parent f86425decfcb3410d5bc90da6ce6146e04775953
author Me <john.smith@example.com> 1399059521 -0700
committer Me <john.smith@example.com> 1399059521 -0700

    What is going on here?

git ls-files -s,这似乎是相关的:

100644 fab05d167a3f8ef9ddf00d7af09e00eea03f1d28 0   requirements.txt
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0   status/__init__.py
100644 923b97d0d9e6c68e77751bb4e7fb14c41fa9d71a 0   status/api/__init__.py
100644 75f064fc9c170b56545c6b92958b38c10260beec 0   status/api/message.py
100644 07cf4dc004231945847e9f93e3de214dd02c3661 0   status/api/message_db.py
100644 78cda37dcc7a4b4f34089b2d843a9abfcd4118cf 0   status/server.py
100644 bee7c1eb7b92ccf6b78c7dfc5f36e71b77ec5792 0   status/util.py
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0   wsgi_util/__init__.py
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0   wsgi_util/api/__init__.py
100644 e92f248874be5beec7b18228d4bf0d062b508916 0   wsgi_util/api/api.py
100644 68a5a62034d52684222c7075f59f2927c1d8695e 0   wsgi_util/api/common.py
100644 28d9add6dae84ddab293e3a44cac4824f8d2d482 0   wsgi_util/api/error.py

另一个线索?

因为这变得很重要,所以我做了一个 git reset --mixed HEAD。然后我开始重新创建索引。其中一个文件 wsgi_util/__init__.py 不为空,但我希望(暂时)将其提交为空文件:其中没有有用的代码。为此,我到目前为止运行了 git add --intent-to-add wsgi_util/__init__.py。我不能提交这个索引。

但是,我发现如果我用空文件替换它,然后执行正常的 git add,生成的索引将提交。

最佳答案

文档说明了关于--intent-to-add

Record only the fact that the path will be added later. An entry for the path is placed in
the index with no content. This is useful for, among other things, showing the unstaged
content of such files with git diff and committing them with git commit -a

你可以在这里自己阅读。 https://www.kernel.org/pub/software/scm/git/docs/git-add.html

根据我对文档的理解和我自己的实验,您需要使用git commit -a 来添加文件。

关于git commit 在索引中留下更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23435065/

相关文章:

git - 为什么 merge 后 GIT 说 "Already up-to-date",但分支之间的差异仍然存在?

git - 是否可以在目录命名空间以外的地方使用 refspec 中的过滤器?

git - 单个 Git 存储库中的公共(public)和私有(private)代码

Linux:两个 git 提交的视觉差异——整个存储库

git - 是否可以使用 github API 获取一个文件的差异?

git - 小型但多目录项目的存储库数量?

git - 克隆时配置文件中的 "bad config line 1"

git - 将配置参数保存到存储库

git - 使用https时如何通过防火墙访问Github扮演中间人?

android - Git:跟踪或取消跟踪 Android API key ?