Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git commit leaves changes in the index

Tags:

git

The Problem

I'm attempting to commit some changes, but they're being left behind, for reasons I do not understand:

I take a look a the current repo state:

% 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

Messy, but what is in the index is what I want to commit. (Note: there are changes that aren't staged: I am not interested in committing those. The changes under "Changes to be committed" are the changes I desire to be committed.) I commit it:

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

And immediately re-run status, to witness nothing has changed:

% 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

If I look at the last commit, we can see that nothing made it in:

% git log -n1 -p
commit a4dbe36e77a9f3b04c2ef33de75e28b6aa7cac0c
Author: Me <[email protected]>
Date:   Fri May 2 11:52:06 2014 -0700

    Why won't you commit?

What is going on here? Why aren't these changes committed?

A minimal testcase!

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

That's just set up. Now for the real fun. I get funky behavior with --intent-to-add (I may just not understand what it does), but this reproduces my full problem:

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

And then attempt to commit:

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

Potentially use outputs

I don't have any custom hooks (that I'm aware of):

% 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

If it matters, the directory the added files are in is new as well, and does not exist in the preceeding commit. (I was in the directory itself when I ran git status, see the .. in the not-staged-for-commit section.)

git diff HEAD a4dbe36e77a, as requested, does not output anything.

git show:

% git show a4dbe36e77a
commit a4dbe36e77a9f3b04c2ef33de75e28b6aa7cac0c
Author: Me <[email protected]>
Date:   Fri May 2 11:52:06 2014 -0700

    Why won't you commit?

Also, the same, in raw:

% git show --format=raw a4dbe36e77a
commit a4dbe36e77a9f3b04c2ef33de75e28b6aa7cac0c
tree ca9326ccba91dda7540198c082db6e6ab3fb097a
parent 1842d56d5a47ff33bd420a5014f208c85acc5a1f
author Me <[email protected]> 1399056726 -0700
committer Me <[email protected]> 1399056726 -0700

    Why won't you commit?

An ls-tree of that tree:

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

For brevity: git diff --staged shows a diff of changes: this is the diff that I desire to be committed, but that git commit isn't committing. (git diff --cached also shows this diff.) git commit -v also shows this diff, but typing a message does not result in a commit.

The only really weird thing about this branch is that it was an orphaned branch — master contains a WIP, but messy version of this code. I was hoping to clean it up, but retain master because I'm human, and worry I'll delete something. However, this isn't the first commit. (The first commit has actual changes, too.) It's only now something's gone wrong.

Potential clue / something isn't right here:

EDIT @torek had me run this:

% git write-tree
ca9326ccba91dda7540198c082db6e6ab3fb097a

This should turn the index's contents into a tree (which could then be attached to a commit object, and thus a commit is born). However, that hash is the same as HEAD. Given that there are changes in my index, one would expect that the tree ID would have to be different. Specifically, these commands show something is odd:

% 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 <[email protected]> 1399059521 -0700
committer Me <[email protected]> 1399059521 -0700

    What is going on here?

git ls-files -s, which seems relevant:

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

Another clue?

Because this was getting significantly in the way, I did a git reset --mixed HEAD. I then proceeded to recreate the index. One of the files, wsgi_util/__init__.py isn't empty, but I wish to commit it (for now) as an empty file: there is no useful code in there. To do this, I have thus far run git add --intent-to-add wsgi_util/__init__.py. I cannot commit this index.

However, I discovered that if I instead replace it with an empty file, and then do a normal git add, the resulting index will commit.

like image 870
Thanatos Avatar asked May 02 '14 18:05

Thanatos


People also ask

How do you undo commit but keep changes git?

In order to undo the last Git commit, keep changes in the working directory but NOT in the index, you have to use the “git reset” command with the “–mixed” option. Next to this command, simply append “HEAD~1” for the last commit.

How do I remove a commit change in git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.

When running git reset the affected files will change state?

Comparatively, git reset , moves both the HEAD and branch refs to the specified commit. In addition to updating the commit ref pointers, git reset will modify the state of the three trees. The ref pointer modification always happens and is an update to the third tree, the Commit tree.


Video Answer


1 Answers

The documentation states this about --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

You can read for yourself here. https://www.kernel.org/pub/software/scm/git/docs/git-add.html

From my understanding of the documentation and experimenting myself, you need to use git commit -a to add the files.

like image 73
Leon Avatar answered Sep 22 '22 13:09

Leon