Search code examples
gitgit-add.git-folder

Why does Git store the file in the repository when I just add it to the staging area?


From what I understand when you git add a file, it merely stages the file before adding it to the repository but why can I see it added into the git repository before I've committed it?

For instance, if I create a new git repository and create a new file called foo and add the contents "hello world" into it, and then git add foo, I see a new item in the objects subdirectory inside the .git folder. I can even view the contents of the new file inside the objects file with the git cat-file -p command.

What exactly has been added to the .git/objects folder? What does staging a file technically do? Like what are the steps that take place after a git add is run on a file? Maybe I'll understand it better if I know the steps.


Solution

  • The staging area is part of the repository.

    I think you are confusing the repository's object database with the history. Only commits are part of the history, but all objects Git handles are part of the object database.

    Think about it: Git doesn't stay resident in memory, so where else would it record what is part of the staging area than in its object database?