Git's Secret Diary: How It Remembers Every Change You Make

10 Minby Muhammad Fahid Sarker
gitversion controlgit tutorialhow git worksgit for beginnersgit addgit commitgit statusgit diffstaging areagit repositoryprogramming basics

The Nightmare of final_project_v2_final_for_real_this_time.js

We’ve all been there. You're working on a project. You save a version. You make some changes. You save another version. Soon, your project folder looks like a cry for help:

  • my-app.js
  • my-app_backup.js
  • my-app_works_before_i_added_that_crazy_feature.js
  • my-app_final.js
  • my-app_final_v2.js

It’s chaos. What if you want to see what you changed between v2 and for_real_this_time? Good luck opening both and playing a game of spot-the-difference. What if you accidentally delete a crucial file? Pray you have a backup.

This is the exact problem Git was born to solve. Git is like a meticulous, slightly obsessive-compulsive diary keeper for your code. It watches your files, notes every single change, and lets you travel back in time to any point in your project's history. But how does it actually do it?

Meet Your Project's Three Magical Realms

To understand Git, you need to understand that your project files can exist in three different states, or as I like to call them, realms. Imagine you're a chef cooking a meal.

Three states of Git

  1. The Working Directory (Your Kitchen Counter): This is your project folder, the files you can see and edit. It's your messy kitchen counter where you're chopping vegetables, making a mess, and trying out new ingredients. It's where the work happens.

  2. The Staging Area (Your Prep Bowl): This is one of the most powerful but initially confusing parts of Git. The Staging Area is like a prep bowl you put your perfectly chopped ingredients into before you add them to the pot. You don't just dump everything from your counter into the pot at once, right? You carefully select what's ready. The Staging Area lets you do the same with your code. You can choose to save changes from file1.js but ignore the messy, half-finished work in file2.js for now.

  3. The Repository (Your Recipe Book): This is the .git directory inside your project. It's the sacred, permanent recipe book. Once you've gathered your prepped ingredients (staged your changes) and you're happy with them, you commit them to the recipe book. This saves a permanent record of that version of your project. You can't change the past entries, but you can always look back at them.

The Big Secret: Git Thinks in Snapshots, Not Differences

Here's where most people get it wrong. They think Git just records the lines you added or deleted (the 'diff'). While that's part of it, it's not the whole story.

Every time you git commit, Git takes a snapshot of your entire project at that moment.

Think of it like a photographer taking a picture of your entire desk. It doesn't just take a picture of the new coffee mug you added; it photographs the whole desk: the mug, the keyboard, the monitor, everything.

"Whoa," you say, "Doesn't that take up a massive amount of space? Taking a full copy of my project every time I save?!"

This is where Git's genius comes in. If a file hasn't changed since the last snapshot, Git doesn't store a new copy of it. Instead, it just includes a link to the previous, identical version of that file. It's like in your photo album, instead of re-printing the same photo of your cat on every page, you just write a little note that says, "Cat is still sleeping, see page 5 for photo."

This makes Git incredibly fast and efficient.

Let's Watch Git in Action

Let's see this in practice. Open up your terminal, create a new folder, and let's go!

1. Hire the Scribe (git init)

First, we need to tell Git to start watching this folder.

bash
# Create a new folder and move into it mkdir git-diary cd git-diary # Tell Git to start tracking this folder git init # Output: Initialized empty Git repository in /path/to/your/git-diary/.git/

This creates a hidden .git folder. This is the repository, the scribe's diary. Don't touch it!

2. Write Our First Draft

Let's create a file.

bash
echo "Once upon a time, in a repo far, far away..." > story.txt

3. Ask the Scribe, "What's Up?" (git status)

git status is your best friend. It tells you exactly what Git is thinking. Use it often!

bash
git status

Git will reply with something like this:

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        story.txt

nothing added to commit but untracked files present (use "git add" to track)

Git is saying, "Hey! I see this new story.txt file here on the kitchen counter (Working Directory), but I'm not tracking it. You haven't told me if it's important yet."

4. Prep the Ingredients (git add)

Let's tell Git that this new file is important and we want to include it in our next save point. We're moving it to the Staging Area.

bash
git add story.txt

Now, let's ask about the status again.

bash
git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   story.txt

See the difference? Git now says this file is in the "Changes to be committed" section. It's in our prep bowl, ready to go into the recipe book.

5. Write it in the Recipe Book (git commit)

Now we take our snapshot. We commit our staged changes to the repository with a descriptive message.

bash
git commit -m "Wrote the first line of our epic story"

Boom! We've saved our first version. git status will now tell you your working tree is clean. Everything is saved and recorded.

6. Making a Change

Let's add to our story.

bash
echo "A brave developer embarked on a quest." >> story.txt

Now, let's use another cool command, git diff, to see exactly what has changed.

bash
git diff
diff
diff --git a/story.txt b/story.txt index 2d3b769..5e835a2 100644 --- a/story.txt +++ b/story.txt @@ -1 +1,2 @@ Once upon a time, in a repo far, far away... +A brave developer embarked on a quest.

Git shows you precisely the line you added! Now you can git add story.txt and git commit -m "Added the hero of the story" to save this new version.

So, Why Is This Awesome?

This system of snapshots and the three realms gives you superpowers:

  • Time Travel: You can instantly revert your entire project back to any previous commit. Messed everything up? No problem. git checkout <commit_id> and you're back to a time when things worked.
  • Fearless Experimentation: The snapshot system makes branching (creating a parallel universe of your project) incredibly cheap and easy. You can create a new branch to try out a crazy new feature, and if it doesn't work, you can just delete the branch. Your main code is never affected.
  • Clear History: By committing small, logical chunks of work, you create a beautiful, understandable history of your project. It's a real diary that tells the story of how your code came to be.

So next time you type git add, you're not just running a command. You're carefully selecting ingredients for your next masterpiece. And when you git commit, you're not just saving a file; you're taking a snapshot, a perfect photograph, to be preserved forever in your project's secret diary.

Related Articles