Git Basic Workflow

πŸ”
Course Under Active Revision Re-visiting

This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.

1. Initializing a Git Repository

Before Git can track changes, you must initialize a repository inside your project directory. This creates a hidden .git folder, which acts as Git’s database for tracking changes.

mkdir test_proj
cd test_proj
git init

2. Interactive Basic Workflow Simulator

Step through the pipeline below to see how files move from untracked modifications on disk to staged index entries and finally permanent commit snapshots:

Git Basic Workflow Pipeline

Step 1 of 4: Initialize Repository
1 / 4
πŸ’» Working Tree Disk Files

Active workspace where files are created & edited.

βœ“ Working tree clean
πŸ“¦ Staging Area Index

Prepared files staged via git add.

βœ“ No files staged
πŸ›οΈ Git Repository .git / HEAD

Immutable snapshots created via git commit.

No commit snapshots yet
$ git init

Creates a new Git repository and sets up the internal .git database for tracking files.


3. Adding Files to a Repository

When you create or edit files in your working directory, use git add to prepare them for a commit:

# Create a new file
touch index.txt

# Tell Git to stage index.txt
git add index.txt

# To stage all modified and new files at once:
git add .

This moves index.txt to the staging area, preparing it for the next commit snapshot.


4. Checking Repository State

Before committing, check which files are being tracked and their current status:

# Check repository status
git status

# View commit history
git log --oneline

5. Committing Changes

Once files are staged, commit them into Git’s repository ledger. Each commit represents a permanent snapshot of the project. The -m flag specifies a descriptive commit message:

git commit -m "Initial Commit"

# Alternatively, add and commit tracked files simultaneously:
# git commit -am "Update app logic"

6. Viewing Changes: git diff vs git diff --staged

Before committing, review modifications made to your files:

Inspecting Unstaged Changes (git diff)

Compares the working directory with the staging area (shows what you edited on disk but haven’t staged yet):

git diff

Inspecting Staged Changes (git diff --staged)

Compares the staging area with the last commit (shows what will be included in your next commit):

git diff --staged

Step through the interactive stages below to see how Git diffs unstaged disk edits versus staged index snapshots:

Inspecting Unstaged vs Staged Changes

Step 1 of 5: Edit Tracked File on Disk
1 / 5
πŸ’» Working Tree Disk Files

Active workspace where files are created & edited.

πŸ“„ index.txt modified
πŸ“¦ Staging Area Index

Prepared files staged via git add.

βœ“ No files staged
πŸ›οΈ Git Repository .git / HEAD

Immutable snapshots created via git commit.

πŸ›οΈ c1a2b3 (Initial Commit) HEAD
$ echo "hello second line" >> index.txt

index.txt is modified in the working tree. Git detects unstaged changes on disk.


7. Ignoring Files with .gitignore

Not every file in your project should be tracked. Sensitive secrets (such as .env), massive dependency trees (node_modules/), build outputs (dist/, target/), and OS files (.DS_Store) should be ignored.

Creating a .gitignore file in your repository root instructs Git to ignore these files completely:

# Node dependencies
node_modules/

# Environment secrets
.env
.env.local

# Build artifacts
dist/
build/
*.log

Ignoring Files with .gitignore

Step 1 of 3: Sensitive or Temporary Files on Disk
1 / 3
πŸ’» Working Tree Disk Files

Active workspace where files are created & edited.

πŸ“„ .env untracked
πŸ“„ app.js untracked
πŸ“¦ Staging Area Index

Prepared files staged via git add.

βœ“ No files staged
πŸ›οΈ Git Repository .git / HEAD

Immutable snapshots created via git commit.

No commit snapshots yet
$ echo "API_KEY=xyz" > .env && touch app.js

Working tree contains private secrets (.env) and source files (app.js). Unignored secrets could leak to public remotes!

How to Untrack an Already Committed File

If a file was accidentally committed before being added to .gitignore, removing it from disk isn’t necessary. You can remove it from Git tracking while keeping it safely on disk using git rm --cached:

# Untrack file from repository while keeping local copy on disk:
git rm --cached .env

# Commit the removal
git commit -m "chore: stop tracking .env secret"