Skip to main content

Command Palette

Search for a command to run...

How It Works and the Role of the .git Folder

Published
3 min read

What is the .git Folder?

When you type git init, a hidden folder called .git is created. This is the "Brain" of your project. If you delete this folder, Git "forgets" everything and your project becomes just a normal folder again.

  • Why it exists: To keep a record of every single change you ever made.

  • Where is it? : It's hidden in your project folder. It's like a secret diary where Git writes down everything.

The Three "Things" Git Stores

Git keeps three types of items. Let's use a "Cooking" example:

  • The Blob (The Ingredients): Git takes the text inside your file and turns it into a "Blob." It doesn't care if the file is named recipe.txt or pizza.txt. It only cares about the words inside.

    • Example: The list of ingredients is the Blob.
  • The Tree (The Recipe Card): This is a list. It says: "The tomato goes in the Veggie folder, and the cheese goes in the Dairy folder." It organizes the Blobs.

    • Example: The Recipe Card tells you which ingredients go where.
  • The Commit (The Photo): This is a photo of the finished meal. It includes the Tree, the date, your name, and a note.

    • Example: A photo of your pizza with a note saying "Made this on Sunday."

What happens when you type?

Step 1: git add (The Shopping Cart)

When you type git add, you are not saving yet. You are just putting your "ingredients" (files) into a shopping cart.

  • Git looks at your file.

  • It makes a Blob.

  • It puts it in the cart and waits.

Step 2: git commit (The Safe)

When you type git commit, Git takes everything in the cart and locks it in a Safe.

  • It makes a Tree (to remember the folders).

  • It makes a Commit (the save point).

  • It gives that save point a "Fingerprint" (the Hash).

The "Fingerprint" (The Hash)

Git is very too careful. It gives every save point a long name made of random letters and numbers (like a1b2c3d4).

  • If you change just one letter in your project, the fingerprint changes completely.

  • This is how Git knows if your work is safe and hasn't been changed by a mistake.

→ How Git Tracks Changes

The Three Crucial "Steps"

To track a change, you have to tell Git three things:

  • Step 1: "I am working!" You are just typing. Git sees your work is different (usually it shows the file name in Red).

  • Step 2: "Look at this" (git add) You pick the changes you like. It's like putting a circle around the best part of your drawing. Now Git shows it in Green.

  • Step 3: "Save this photo" (git commit) Git takes a permanent photo of the paper. It puts it in a book with a date and a note.

How does Git know if I changed something?

Git is like a super-detective with a magnifying glass.

  1. It turns your file into a Number.

  2. If the file is "Hello," the number might be 123.

  3. If you change it to "Hello chaicode", the number mgiht becomes 999.

  4. Git just looks at the number. If the number changes, Git knows you changed the file.