0

Note: This is purely learning purpose post

I am going to use git init to create a git repository in my local, and here it is enter image description here

Notice here I have .git and object folder, which will be used to store snapshot of my commits and staging. Now I will go ahead and add one new file i.e. stage file1, and notice it creates one object with hash code for the stage enter image description here

Now this object is a reference for what will be committed, but if I delete this object and run git status , git still shows file1 as staged but actually there is not object reference now for that
enter image description here

file1 is still there with the same changes but actually git cant not commit that as no object found but why git still shows file1 as staged or ready for commit enter image description here

what is the reference for git status here ?

StaticVariable
  • 396
  • 2
  • 10

1 Answers1

2

To my understanding, git status is a result of git diff commands run twice:

  1. git diff --cached: compares the current (HEAD) commit to the staging-area, and

  2. git diff with no additional arguments compares the staging-area to the work-tree.

I did the same thing (as you did) i.e. modified a file and deleted the corresponding object inside the object directory of .git directory and ran git-diff --cached --name-only which showed me the file name which I had modified.

So, even if you've deleted the object file, git status will still show you the file staged while it's not able to commit as it looks for the newly created object hash in order to build a commit.

In order to see the behaviour, here's what I did:

  1. git diff-index --cached HEAD (Compare a tree to the working tree or index) enter image description here

  2. git ci -m 'bob' enter image description here

You can see how the modified object hash is still present in git cache and while doing git commit, git looked for the same modified object hash which I deleted earlier and git threw error.

Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
  • I voted up for your answer which is correct but since I posted this for learning so I would prefer more detail answer as accepted answer. https://stackoverflow.com/a/36927101/5448364 this does explain everything what I needed. Thank you – StaticVariable Sep 20 '18 at 06:34