Git Core Commands
Git is a distributed version control system. These are the day-to-day commands you will use in every project - mastering them is essential for any developer workflow.
Initializing & Staging
Every Git workflow starts with initializing a repository and staging changes. The staging area (index) lets you build up exactly the changes you want in your next commit.
# Initialize a new repository
git init
git init my-project # Create new directory and init inside it
# Clone an existing repo
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder # Into specific folder
git clone --depth 1 https://github.com/user/repo.git # Shallow clone (faster)
# Check working tree status
git status
git status -s # Short/compact format
# Stage files
git add file.txt # Stage specific file
git add src/ # Stage entire directory
git add . # Stage all changes in current directory
git add -p # Interactively stage hunks (patch mode)
git add -u # Stage only tracked files (no new files)
# Unstage files
git restore --staged file.txt # Unstage (modern syntax)
git reset HEAD file.txt # Unstage (older syntax)Committing Changes
Commits are snapshots of your project. A good commit message describes what changed and why. Use the imperative mood: "Add feature" not "Added feature".
# Commit staged changes
git commit -m "Add user authentication"
# Stage all tracked files and commit in one step
git commit -am "Fix typo in README"
# Open editor for a multi-line commit message
git commit
# Amend the last commit (before pushing!)
git commit --amend -m "Corrected commit message"
git commit --amend --no-edit # Amend without changing the message
# Empty commit (useful for triggering CI)
git commit --allow-empty -m "Trigger CI build"Viewing History & Differences
git log and git diff are your windows into the project history and current state of changes.
# View commit log
git log
git log --oneline # Compact one-line format
git log --oneline --graph # ASCII branch graph
git log --oneline -10 # Last 10 commits
git log --author="Alice" # Filter by author
git log --since="2 weeks ago"
git log -- src/auth.ts # History for a specific file
# View differences
git diff # Unstaged changes vs last commit
git diff --staged # Staged changes vs last commit
git diff HEAD # All uncommitted changes
git diff main..feature # Compare two branches
git diff abc123 def456 # Compare two commits
git diff HEAD~1 # Compare with one commit agoUndoing Changes
Git gives you multiple ways to undo mistakes. Choose the right tool depending on whether you have pushed changes or not.
# Discard changes in working directory
git restore file.txt # Discard unstaged changes (modern)
git checkout -- file.txt # Discard unstaged changes (older)
# Reset commits (do NOT use on pushed commits)
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --mixed HEAD~1 # Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1 # Undo last commit, discard all changes
# Revert a commit (safe - creates new commit to undo)
git revert abc123
git revert HEAD # Revert last commit
# Stash uncommitted changes temporarily
git stash # Stash working changes
git stash push -m "WIP: auth feature" # Named stash
git stash list # List all stashes
git stash pop # Apply most recent stash and remove it
git stash apply stash@{1} # Apply specific stash without removing
git stash drop stash@{0} # Delete specific stash
git stash clear # Delete all stashesKeep your own version of these notes — editable, searchable, and organised by your stack.
Start free