gitgithubgit-commitrmgit-rm

how can git commit -a work after i remove a file from index


Suppose I have tracked file empty.txt. Now I remove file from working directory using rm empty.txt . Now I am removing file from index using git rm empty.txt and now file should be untracked since I have removed it from index. After this I used git commit -a -m "remove empty.txt" but how it is working as -a parameter is used to skip explicitly adding tracked file to staging area but I already removed file from index? It should be untracked.


Solution

  • You have two possible misconceptions:

    If you take a photograph of a dog standing among two trees, the photograph contains a dog and two trees. That is a commit.

    If the dog walks away and you take another photograph, the photograph contains the two trees but not the dog. It wasn't there when you took that photograph. That is another commit.

    There is no "removal" action involved in the creation of the second commit. It isn't a modification of the first photograph. It's just a photograph of what is.

    In the case of a git commit that you create, "what is" is the index, plain and simple (by default; there are ways around this, by saying git commit someFile, but ignore that for our purposes).


    Okay, so, on to the details of your question. You say:

    i remove file from working directory using rm empty.txt

    now i am removing file from index using git rm empty.txt

    after this i used git commit -a

    So: