Max Koretskyi
Max Koretskyi

Reputation: 105499

how does git know that file is staged for deletion?

I've committed a file:

    $ git init
    $ echo 'content here' | git hash-object -w --stdin 
      ab9fb23311ea27af88324b66a5a00c826fdcfbb8
    $ git update-index --add --cache 100644 \
      ab9fb23311ea27af88324b66a5a00c826fdcfbb8 file1.text
    $ git commit -m "setting up"
    $ git checkout-index -a

Checking that everything is OK:

$ git status
On branch master
nothing to commit, working directory clean

Now if I remove the file from working directory and run git status git will tell me that file is deleted BUT not staged for commit:

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    file1.text

no changes added to commit (use "git add" and/or "git commit -a")

Since deleting file in working directory doesn't remove it from staged area in index - ls-files --stage still shows it, I assume that the fact that the file is deleted is determined by simply running comparison of what is now in staged area of index - the output of ls-files --stage and what is in working tree. So here everything is clear. But then I run this:

$ git rm file1.text
rm 'file1.text'

And the file is actually removed from index - ls-files --staged doesn't show it. And if I run now git status I get the following:

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

        deleted:    file1.text

So git now knows that it is staged for deletion. If run git commit git will create new tree without this file. So my question is how does git know that the file is staged for deletion if it's not listed in the staged index? What does is compare current index file with? Does it compare it with previous tree and defines missing files as staged for deletion, and new files as staged for addition?

Upvotes: 0

Views: 69

Answers (1)

Ismail Badawi
Ismail Badawi

Reputation: 37177

You have it right -- when you run git status, the Changes not staged for commit part is computed by comparing the working directory and the index, and the Changes to be committed part is computed by comparing the index and the tree associated with the commit pointed to by the HEAD ref.

Upvotes: 2

Related Questions