Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DEV Community

Cover image for Commit to Code: Creating Commits & Exploring the .git Folder
Shane Dsouza
Shane Dsouza

Posted on • Edited on

Commit to Code: Creating Commits & Exploring the .git Folder

📚 Git & GitHub Series

➡️ You're reading Part 3

⬅️ Part 2: Setting Up Git and GitHub


Getting Started with Git Tracking

Creating a Git-tracked folder

Create a folder named Git-Learning and add a file (e.g. hello.txt) with some text.

At this point, Git is not tracking this folder or file. Git doesn’t track every folder on your machine by default — you need to initialize it.


Initializing Git

To allow Git to start tracking this project, run:

git init
Enter fullscreen mode Exit fullscreen mode

Git init command output

Git now tracks this folder.

You'll see a U next to the file name, meaning Untracked.


How Does Git Track?

Once git init is run, Git creates a hidden .git folder — the brain of Git. It contains everything about your project's history.

The hidden .git folder


Staging Files

When a file needs to be tracked it is moved over to the Staging Area.

To track a specific file:

git add hello.txt
Enter fullscreen mode Exit fullscreen mode

To track all files:

git add .
Enter fullscreen mode Exit fullscreen mode

Now files will show A, meaning Added to staging.

Staging with git add


What’s Inside the .git Folder?

Exploring .git contents

Some key components:

  • index — tracks files in the staging area.
  • objects — where Git stores commit data.
  • HEAD — points to the current branch or commit.

Let's try reading the index file:

Reading the index file

Use this to view what's staged or untracked:

git status
Enter fullscreen mode Exit fullscreen mode

Output of git status

Git reads from the index file to show you what's staged, modified, or untracked.


Creating a Commit

A commit is like a snapshot of your code at a specific moment:

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Commit created

Each commit generates a unique hash ID and stores:

  • Author name and email
  • Date and time
  • Commit message Reference to the previous commit (if any)

To view the commit history:

git log
Enter fullscreen mode Exit fullscreen mode

Image description


Making Changes

Edit hello.txt, then check what changed:

git diff
Enter fullscreen mode Exit fullscreen mode

git diff output

Git tracks the difference between the working directory and staging area using metadata inside .git.


Diving into the Objects Folder

Inside .git/objects, Git stores:

  • Blobs (file content)
  • Trees (directories)
  • Commits (snapshots)

The folder names are based on the first 2 characters of the SHA-1 hash of the file or commit.

.git/objects structure


Viewing Commit Data

You can use the commit hash to view commit details:

Image description

Notice, I can now see the who committed details, using the commit Hash ID.

Now I want to see the content in the file;
Image description

I will now use the blob ID to see the content in “file1.txt” that was committed.

Image description

Git stores everything in the .git folder — and you rarely need to manually look inside, because Git gives us tools like:

  • git stat
  • git diff
  • git log

Committing

Create another file, say file2.txt, add some content, then:

git add file2.txt
git commit -m "Added file2.txt"
Enter fullscreen mode Exit fullscreen mode

Check the commit log again:

git log
Enter fullscreen mode Exit fullscreen mode

Image description

You'll notice the HEAD is now pointing to the latest commit (Commit 2).


How Git Stores Commits

Every commit contains a SHA-1 hash, generated based on:

  • The content of the files
  • Previous commit
  • Commit message
  • Metadata

This ensures every commit is unique, even if you commit the same code again, you’ll likely get a different hash.

🧠 Git = Content-addressable storage.


Fun Fact: Git Meets Algorithms

Spotify once used an algorithm named MAYA (Most Advanced Yet Acceptable) to recommend music.
While it was supposed to suggest songs related to your taste, a bug caused it to occasionally show completely unrelated tracks, and surprisingly, users loved the variety!

Similarly, Git’s algorithms may look simple, but the power lies in how uniquely and efficiently it stores and suggests changes.


Traveling Between Commits

To navigate through your commit history, first run:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Image description

To compare differences between two commits:

git diff <commit1> <commit2>
Enter fullscreen mode Exit fullscreen mode

Image description


Moving HEAD to a Previous Commit

Want to move back in time?
You can checkout an earlier commit using its hash:

git reset <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Image description

As we move the head to commit 1, the files that were in the latest commit (Commit 2) are in Changes now.

If you don't want the changes then;

git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Image description

Notice because file 2, was added after the second commit, it appears as a new Untracked file, even though it was committed.

To verify:

git log 
Enter fullscreen mode Exit fullscreen mode

Image description


GitHub: The Remote Repository

Sharing the .git folder allows others to see your commits. Instead, use GitHub!

GitHub 1

Initially both developers will have the same content in “file1.txt”. Now when they both begin to work on it simultaneously there will be conflicts as both as using the same file. Now which file is the Source Of Truth ? For that pen drive won’t be needed, we can use Github.

Github 2

Create a GitHub repo (e.g. Git-Masterclass)

Then, link your local project:

Use the push an existing repository from the command line commands

git remote add origin https://github.com/yourname/Git-Masterclass.git
Enter fullscreen mode Exit fullscreen mode

Conflicting versions of the same file

Verify the remote was added by viewing the config file within the .git folder

Remote URL added

To see the URLS of the remotes connected to the local git repository, both for fetching(pull) and pushing.

Checking config file

Follow the remaining commands on GitHub:

git branch -M main git push -u origin main
Enter fullscreen mode Exit fullscreen mode

git remote -v output

Now visit Github will be able to see “Commit 1”, which was on our local machine is now on github.

Commit now visible on GitHub


Wrapping Up

In this post, you learned how:

  • How Git tracks and stages files
  • How commits and hashes work
  • What lives inside the .git folder
  • How to view, reset, and compare commits
  • How to push changes to GitHub

📚 More from the Series


Enjoyed this?

Let me know in the comments or follow me for the next post in this Git & GitHub series!

shanedsouza.com

Top comments (0)