Git Notes
Learn about local Git repositories, the working directory, staging area, and local commit history. Understand how Git manages files on your machine.
Every Git repository on your computer is self-contained and fully functional. It does not need a network connection, a server, or anyone else's permission to operate. This is what makes Git a distributed version control system — your local repository has the complete project history, every branch, every tag, and every commit ever made.
Understanding the internal structure of your local repository is fundamental to mastering Git. When you know where things live and how they move between states, commands like git add, git commit, and git reset stop being mysterious incantations and start being logical operations.
The Three States of Git
Git manages your files through three distinct areas. Every file moves through these states during the development cycle:
| Working | Staging | .git | ||||
|---|---|---|---|---|---|---|
| Directory | ──► | Area | ──► | Repository | ||
| (Index) | (History) |
Working Directory
The working directory is simply the files you see in your project folder. These are the actual files on your filesystem that you open in your editor, modify, and save:
# See the current state of your working directory
git statusOn branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
modified: src/app.js
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
src/newfile.jsFiles here are in one of four states: untracked (Git does not know about them), unmodified (same as last commit), modified (changed but not staged), or staged (ready for commit).
Staging Area (Index)
The staging area is Git's preparation zone. It holds the exact snapshot of what will go into your next commit. This intermediate step exists because you often do not want to commit everything you have changed:
# Stage specific changes
git add src/app.js
git statusChanges to be committed:
(use "git restore --staged <file>..." to unstage)
modified: src/app.js
Changes not staged for commit:
modified: README.mdThe staging area lets you craft precise, focused commits. You can have ten modified files but only commit three of them — the ones that belong together logically.
Repository (.git Directory)
The .git directory is where Git stores everything: commit objects, tree structures, blob content, branch pointers, and configuration. When you run git commit, a permanent snapshot moves from the staging area into this history:
git log --oneline -5a1b2c3d (HEAD -> main) feat: add user authentication b2c3d4e refactor: extract validation helper c3d4e5f fix: resolve navigation z-index bug d4e5f6g feat: implement search functionality e5f6g7h docs: update README with setup instructions
Creating a Local Repository
Starting from Scratch
mkdir my-project
cd my-project
git initInitialized empty Git repository in /home/user/my-project/.git/
At this point you have an empty repository. No files are tracked until you explicitly add and commit them:
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"Cloning an Existing Repository
git clone https://github.com/user/existing-project.git
cd existing-projectCloning creates a local repository with full history, sets up remote tracking to origin, and checks out the default branch. It is the most common way to start working on an existing project.
Inside the .git Directory
Understanding this structure helps demystify Git operations:
ls -la .git/.git/ ├── HEAD # Points to current branch (e.g., refs/heads/main) ├── config # Repository-specific configuration ├── description # Used by GitWeb (rarely relevant) ├── hooks/ # Client-side or server-side scripts ├── index # The staging area (binary file) ├── objects/ # All content (commits, trees, blobs) │ ├── pack/ # Packed objects for efficiency │ └── info/ ├── refs/ # Branch and tag pointers │ ├── heads/ # Local branches (main, develop, etc.) │ ├── remotes/ # Remote-tracking branches │ └── tags/ # Tag references └── logs/ # Reflog history for HEAD and branches
The HEAD file is particularly important — it tells Git which branch you are currently on:
cat .git/HEADref: refs/heads/main
Working Locally Without a Remote
Git works perfectly offline. You get full version control without any server:
git init
git add .
git commit -m "Initial commit"
# Create branches and switch between them
git branch feature/experiment
git checkout feature/experiment
# Commit, merge, view history — all offline
git commit -m "Experimental change"
git checkout main
git merge feature/experiment
git log --graph --onelineThis is useful for personal projects, learning Git, or when you have no internet connection but still want version control.
Inspecting Your Local Repository
# Count objects in your repo
git count-objects -v
# See all local branches
git branch
# See the complete configuration
git config --list --local
# Check repository integrity
git fsckCommon Mistakes
Mistake 1: Deleting the .git directory. This permanently destroys all version history. If you delete .git, your project folder becomes just regular files with no Git capabilities.
Mistake 2: Confusing working directory state with repository state. Just because a file exists in your folder does not mean it is in Git. Untracked files are invisible to Git until you add them.
Mistake 3: Not understanding that git add stages a snapshot. If you modify a file after staging it, the staged version is the old one. You need to git add again to stage the new changes.
Interview Questions
Q1: What are the three main areas of a local Git repository?
The working directory (actual files you edit), the staging area or index (preparation zone for the next commit), and the .git repository (permanent storage of committed history, objects, and metadata). Files flow from working directory through staging into the repository.
Q2: What is the staging area and why does Git have it?
The staging area is an intermediate zone that lets you selectively choose which changes go into a commit. It enables crafting focused, logical commits even when you have modified many unrelated files. Without it, every commit would include all changes, making history messy.
Q3: Can you use Git without a remote repository?
Yes. Git is fully functional locally with branching, committing, merging, history viewing, stashing, and every other operation. A remote is only needed for sharing work with others or maintaining an off-machine backup. Many developers use local-only repos for personal projects.
Q4: Where is the local repository data stored?
In the .git/ hidden directory at the root of your project. It contains objects (blobs, trees, commits), refs (branch and tag pointers), the index (staging area), configuration, and reflog history. Deleting this directory destroys all version control data.
Q5: What does HEAD point to in a local repository?
HEAD normally points to the current branch reference (like refs/heads/main), which in turn points to the latest commit on that branch. When HEAD points directly to a commit hash instead of a branch, you are in "detached HEAD" state.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Local Repository - Understanding Your Git Working Environment.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, working, with, repositories, local
Related Git & GitHub Topics