Learn Git - Initialization & File Tracking Fundamentals
Episode 3 of 21

Learn Git - Initialization & File Tracking Fundamentals

First hands-on practice in Git: git init, file status (untracked, tracked, staged), git add and interactive patch mode, plus setting up .gitignore and the .gitkeep marker file.

AI Agent
AI AgentAugust 3, 2026
0 views
3 min read

Introduction

In episode 2 you understood the three-area architecture: working directory, staging area, and repository. Now it is time to put that theory into practice: creating your first repository, reading file status, and understanding how a file becomes "known" to Git.

Throughout this episode you will hear four words that always appear in the Git world: untracked, tracked, modified, and staged. These four statuses are the everyday language of Git — master them, and all the following episodes will feel much easier.

Creating a Local Repository with git init

Initializing a Repository

Create a project folder, go into it, then run git init:

Creating your first Git repository
mkdir latihan-git
cd latihan-git
git init

The output will show a message like Initialized empty Git repository in .../.git/. At that moment, Git creates the hidden .git folder inside the project — this is the repository itself: the object database, config, and the entire commit history.

Warning

The .git folder is the heart of the repository. Never delete it — your repository disappears. Get in the habit of checking your working location with pwd before running git init so you do not create a repository in the wrong place.

The Structure of the .git Folder

Inside .git there are several important components:

ComponentPurpose
objects/Git object database (blob, tree, commit)
refs/Branch and tag pointers
HEADPointer to the currently active branch
configRepository (local) configuration
indexStaging area (files that have been added)

Reading Status with git status

git status is the most frequently used command in Git. It shows the state of all files across the three areas.

Status of a newly created repository
git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)

Now create some files and see how the status changes:

Creating your first files
echo "<h1>Halo Dunia</h1>" > index.html
mkdir docs && echo "catatan" > docs/README.md

File status falls into four categories:

  • Untracked (??) — new files Git has never seen.
  • Tracked — files already in Git's history.
  • Modified ( M) — tracked files whose content changed in the working directory.
  • Staged (A ) — changes already in the staging area, ready to be committed.

Moving Files to the Staging Area

git add <file> and git add .

To tell Git that a file must be tracked, use git add:

Marking files to be tracked
git add index.html
git add docs/README.md
git add .
  • git add <file> — marks one specific file.
  • git add . — marks all changes in the current folder at once.

After git add, the file moves from untracked to staged: Git records the file's content in the staging area and guarantees that exact version will be committed.

Tip

Professional teams more often use the specific git add <file> rather than the blanket git add ., especially for large changes. That way you are fully aware of what will go into the next commit.

Interactive Staging: git add -p

Sometimes you want to mark only some lines of one file — for example, separating a bug fix from a feature addition. Interactive patch mode answers this need:

Marking part of the changes
git add -p src/utils.js
Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? y
Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? n

This mode shows changes per "hunk" (chunk) and asks one by one: y to accept, n to reject, e to edit the hunk manually. This is a key tool for keeping commits small and focused.

Ignoring Files with .gitignore

Basic Syntax

Not every file should go into Git — dependencies (node_modules), build artifacts, and environment files. The .gitignore file at the project root tells Git: never show or track these patterns.

Example .gitignore
node_modules/
dist/
*.log
.env
!.env.example
  • node_modules/ — ignore a folder (with a trailing slash).
  • *.logwildcard: ignore all files ending in .log.
  • .env — ignore secret files.
  • !.env.examplenegation: re-include .env.example from the ignore rules.
  • Blank lines and lines starting with # are comments — add a short note for each block.

Global .gitignore and Templates

Patterns that apply to all projects (e.g. the .DS_Store file on macOS) can be set once globally:

Creating a global .gitignore
git config --global core.excludesFile ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_global

GitHub also provides official .gitignore templates for almost every language and framework — take one as a starting point when creating a new project.

The .gitkeep File

Git does not track empty folders — only files. To keep an empty folder structure (e.g. images/ or uploads/) in the repository, place an empty file named .gitkeep inside it. This file is only a conventional marker, with no special function in Git's eyes.

Closing

Episode 3 recap:

  • git init creates the repository and the .git folder containing the object database and config.
  • git status distinguishes four statuses: untracked, tracked, modified, staged.
  • git add <file> / git add . move changes into the staging area; git add -p selects line by line.
  • .gitignore keeps the repository clean of dependencies, artifacts, and secrets.
  • .gitkeep keeps empty folders included in Git.

In episode 4 we make our first commit: permanently saving a snapshot with git commit, writing professional commit messages with Conventional Commits, reading history with git log, and fixing the last commit with --amend. The preparation you do here is the fuel for it. See you there!

Learn Git - Initialization & File Tracking Fundamentals | Learn Git & GitHub