Basic Git Commands
Master the core Git workflow: init, status, add, commit, log, diff, and undo — the commands you will use every single day.
Introduction
There are hundreds of Git commands, but you only need about ten to handle 90% of your daily work. This lesson covers the essential commands in the order you use them in a real project. Run every example yourself — muscle memory matters more than reading here.
Starting a Repository
git init — start tracking a folder
# Navigate to your project folder
cd my-project
# Initialise a new Git repository
git init
# Output: Initialized empty Git repository in .../my-project/.git/
This creates a hidden .git folder. Everything Git knows about your project is stored there. Never manually edit or delete it.
git clone — copy an existing repository
# Clone a repository from GitHub (HTTPS)
git clone https://github.com/username/repo-name.git
# Clone and rename the local folder
git clone https://github.com/username/repo-name.git my-folder
Cloning downloads the full history, not just the latest files.
The Core Workflow
The everyday Git cycle is: make changes → stage → commit → repeat.
git status — what is going on?
git status
Run this constantly. It shows:
- Which branch you are on
- Files that are modified (but not staged)
- Files that are staged (ready to commit)
- Untracked files (new files Git doesn't know about yet)
# Example output
On branch main
Changes not staged for commit:
modified: index.html
Untracked files:
style.css
git add — stage changes
# Stage a specific file
git add index.html
# Stage all changes in the current directory and below
git add .
# Stage parts of a file interactively (advanced)
git add -p
git add . stages all modified and new files. If you only want to commit some changes, stage files individually.git commit — save a snapshot
# Commit with a short message (most common)
git commit -m "Add navigation menu"
# Stage all tracked files AND commit in one step
git commit -am "Fix typo in hero text"
# Note: -a does NOT add untracked (new) files
A commit message should complete the sentence: "If applied, this commit will ___". So: "Add login form", "Fix broken layout on mobile", "Remove unused CSS".
Inspecting History
git log — view the commit history
git log
# Shows full commit history with IDs, author, date, message
git log --oneline
# Compact: one commit per line
# abc1234 Add navigation menu
# def5678 Fix hero button colour
# 9ab0123 Initial commit
git log --oneline --graph
# Shows branch/merge history as ASCII art
git show — inspect a specific commit
# Show what changed in the most recent commit
git show
# Show a specific commit by its ID
git show abc1234
git diff — what has changed?
# Diff between working directory and last commit (unstaged changes)
git diff
# Diff between staged area and last commit (staged changes)
git diff --staged
# Diff between two commits
git diff abc1234 def5678
Lines starting with + were added; lines starting with - were removed.
Undoing Changes
One of Git's most valuable features is the ability to undo mistakes.
Discard unstaged changes in a file
# Restore a file to its last committed state (DESTRUCTIVE — you lose the changes)
git restore index.html
# Older syntax (same effect)
git checkout -- index.html
Unstage a staged file
# Remove a file from the staging area (keeps your changes in the file)
git restore --staged index.html
Amend the last commit
# Fix the message of the most recent commit
git commit --amend -m "Corrected commit message"
# Add a forgotten file to the most recent commit
git add forgotten-file.css
git commit --amend --no-edit # reuse the existing message
Revert a commit (safe undo)
# Create a NEW commit that undoes a previous commit
# This is safe because it does not rewrite history
git revert abc1234
git reset (use carefully)
# Undo the last commit but keep the changes (staged)
git reset --soft HEAD~1
# Undo the last commit and unstage changes (keep files)
git reset HEAD~1
# Undo the last commit and DISCARD all changes (DESTRUCTIVE)
git reset --hard HEAD~1
Quick Reference
git init # Start a new repository
git clone <url> # Copy an existing repository
git status # Check what's changed
git add <file> # Stage a file
git add . # Stage all changes
git commit -m "message" # Save a snapshot
git log --oneline # View history
git diff # See unstaged changes
git diff --staged # See staged changes
git restore <file> # Discard file changes
git restore --staged <file> # Unstage a file
git commit --amend # Fix last commit
git revert <id> # Safely undo a commit
Common Mistakes
- Forgetting
git addbefore committing. Rungit statusfirst to confirm which files are staged. - Using
git reset --hardcarelessly. This permanently deletes uncommitted work. There is no undo. - Amending pushed commits. This rewrites history and forces your collaborators to resolve conflicts. Never amend a commit that is already on a shared branch.
- Committing the entire project in one huge commit. Make smaller, focused commits so the history is readable and reversals are surgical.
Practice Exercise
- Create a new folder called
git-practice, navigate into it, and rungit init. - Create a file
index.htmlwith some basic HTML. Rungit status. - Run
git add index.htmland thengit statusagain. Notice the difference. - Run
git commit -m "Add index.html". - Make a change to
index.html. Rungit diffto see the change. - Stage and commit the change with a descriptive message.
- Run
git log --onelineto see your two commits. - Make another change, then run
git restore index.htmlto discard it.
Assignment
Take one of your mini project folders (e.g., the Digital Clock from Project 01). If it is not already a Git repository:
- Run
git initinside the folder. - Run
git add .to stage everything. - Run
git commit -m "Initial commit: Digital Clock project". - Make a small improvement to the project (e.g., change the font or add a colour).
- Commit the change with a descriptive message.
- Run
git log --onelineand verify you have two commits.
Interview Questions
- What is the difference between
git addandgit commit? —git addmoves changes to the staging area;git commitpermanently saves everything in the staging area as a new snapshot. - What does
HEADmean in Git? — HEAD is a pointer to the current commit (usually the tip of the current branch).HEAD~1means "one commit before HEAD". - What is the difference between
git revertandgit reset? —revertcreates a new commit that undoes a previous one (safe for shared branches);resetmoves the branch pointer back in history, which rewrites history (unsafe for shared branches). - How do you see what changed between two commits? —
git diff <commit1> <commit2>