Published May 23, 2026 · 10 min read · 🏷️ Version Control
Git Commands Handbook: From Basics to Advanced
Git is the backbone of modern software development. This guide covers essential commands from basic commits to advanced workflows like interactive rebase and bisect.
Basic Commands
Initialize a new repository:
git init
git clone https://github.com/user/repo.git
Making Changes
Stage and commit files:
git add file.txt
git add . # stage all changes
git commit -m "Add feature"
git commit -am "Fix bug" # stage modified tracked files and commit
View the current state:
git status
git diff # unstaged changes
git diff --staged # staged changes
git diff HEAD~1..HEAD # diff between commits
Working with Branches
List, create, and switch branches:
git branch # list local branches
git branch -a # list all branches (including remote)
git branch feature/login # create new branch
git checkout feature/login
git switch feature/login # newer syntax
git switch -c feature/login # create and switch in one command
Rename and delete branches:
git branch -m old-name new-name
git branch -d feature/login # safe delete (merged only)
git branch -D feature/login # force delete
Merging and Rebasing
Merge a branch into current branch:
git merge feature/login
git merge --no-ff feature/login # create merge commit even for fast-forward
Rebase to keep history linear:
git rebase main
git rebase -i HEAD~3 # interactive rebase (rewrite last 3 commits)
Golden Rule: Never rebase commits that have been pushed to a shared branch. Rebase only your own local commits.
Working with Remote
git remote -v
git remote add origin https://github.com/user/repo.git
git fetch origin
git pull origin main
git push origin main
git push -u origin feature/login # set upstream
Stashing Changes
Save changes temporarily without committing:
git stash
git stash pop # apply and remove stash
git stash apply # apply but keep stash
git stash list
git stash drop stash@{0} # remove specific stash
Time Travel: Undo Changes
Undo working directory changes:
git checkout -- file.txt # discard unstaged changes
git restore file.txt # newer syntax
git restore . # discard all unstaged changes
Undo staged changes:
git reset HEAD file.txt
git restore --staged file.txt
Undo commits (use with caution):
git commit --amend # modify last commit message/content
git revert HEAD # create new commit that undoes last
git reset --soft HEAD~1 # uncommit but keep changes staged
git reset --hard HEAD~1 # uncommit and discard changes
Finding Bugs with Bisect
Use binary search to find the commit that introduced a bug:
git bisect start
git bisect bad # current commit is broken
git bisect good v1.0.0 # this version was fine
# Git will checkout middle commit
# Test and mark as good/bad
git bisect reset # finish bisect session
Useful Aliases
Set up shortcuts for common commands:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --decorate"
Advanced: Interactive Rebase
Squash, edit, or reorder commits:
git rebase -i HEAD~5
# In the editor:
pick abc123 Add login feature
squash def456 Fix typo
reword 789xyz Update readme
# Save and close
Daily Workflow Example
git switch -c feature/dashboard
# Make changes
git add .
git commit -m "Add dashboard stats"
git push -u origin feature/dashboard
# Create PR on GitHub, request review
# After merge:
git switch main
git pull origin main
git branch -d feature/dashboard