Git Basic Workflow
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.
Table of Contents
10 sections
- 1. Initializing a Git Repository
- 2. Interactive Basic Workflow Simulator
- 3. Adding Files to a Repository
- 4. Checking Repository State
- 5. Committing Changes
- 6. Viewing Changes: git diff vs git diff --staged
- Inspecting Unstaged Changes (git diff)
- Inspecting Staged Changes (git diff --staged)
- 7. Ignoring Files with .gitignore
- How to Untrack an Already Committed File
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 RepositoryActive workspace where files are created & edited.
Prepared files staged via git add.
Immutable snapshots created via git commit.
$ 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 DiskActive workspace where files are created & edited.
Prepared files staged via git add.
Immutable snapshots created via git commit.
$ 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 DiskActive workspace where files are created & edited.
Prepared files staged via git add.
Immutable snapshots created via git commit.
$ 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"