WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Advanced · question 42 of 100

How does Git store data internally, and what is the role of the Git object database?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

Git stores data internally using its own unique data structure. When you add a file to Git, it creates a new blob object to represent the contents of the file. When you create a new directory, Git creates a new tree object to represent the directory’s contents. When you make a new commit, Git creates a new commit object that points to the tree object representing the repository’s state at the time of the commit.

All of these objects are stored in the Git object database, which is essentially a content-addressable key-value store. Each object is given a unique SHA-1 hash, which serves as its key in the database. The contents of the object are stored as a value associated with the key. When you reference an object in Git, you do so using its SHA-1 hash.

The Git object database is stored in the .git/objects directory of your repository. Each object is stored in a separate file in this directory, with the first two characters of the file name representing the first two characters of the object’s SHA-1 hash. The remaining characters of the file name represent the rest of the object’s SHA-1 hash.

Here’s an example of how Git stores data internally:

    echo "Hello, world" > hello.txt
    git add hello.txt
    git commit -m "Add hello.txt"

When you run these commands, Git creates a new blob object to represent the contents of hello.txt. It then creates a new tree object to represent the root directory of the repository, and adds a pointer to the blob object from the tree object. Finally, it creates a new commit object that points to the tree object.

All of these objects are stored in the Git object database. You can view the contents of the object database using the git cat-file command:

    git cat-file -p <object-hash>

Where <object-hash> is the SHA-1 hash of the object you want to view.

In summary, Git stores data internally using its own unique data structure. Each object is given a unique SHA-1 hash and stored in the Git object database. The role of the Git object database is to provide a content-addressable key-value store for Git objects. By understanding how Git stores data internally, you can gain a better understanding of how Git works and how to use it effectively.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics