Git Commands
Quick-reference guide to the most common Git commands — configuration, branching, merging, remote operations, undoing changes, stashing, and inspection.
Git is a distributed version control system that tracks changes to files and enables collaboration through branching, merging, and remote repositories. Every project folder under Git control has a .git directory — the local repository that stores all history.
Note: Commands shown below assume Git ≥ 2.x. Some newer options (like git switch and git restore) were added in Git 2.23.
Configuration
| Command | What it does |
|---|---|
| git config --global user.name "Name" | Set your name for commits (global) |
| git config --global user.email "e@mail" | Set your email for commits (global) |
| git config --global init.defaultBranch main | Default branch name for new repos |
| git config --global alias.co checkout | Create a shortcut: git co = git checkout |
| git config --list | Show all current config values |
| git init | Create a new Git repository in the current directory |
| git init repo-name | Create a new repo in a new directory |
Working with Changes
| Command | What it does |
|---|---|
| git status | Show working tree status (changed, staged, untracked files) |
| git add file | Stage a specific file for the next commit |
| git add . | Stage all changes in the current directory |
| git add -p | Stage changes interactively per hunk (partial add) |
| git commit -m "msg" | Commit staged changes with a message |
| git commit -am "msg" | Stage all tracked files + commit (skip git add) |
| git commit --amend | Edit the last commit message or add staged changes to it |
| git diff | Show unstaged changes (working tree vs index) |
| git diff --staged | Show staged changes (index vs last commit) |
| git diff main..feature | Compare two branches |
| git rm file | Remove a file from tracking + delete it from disk |
| git mv old new | Rename or move a file (stages the change) |
Branching & Merging
| Command | What it does |
|---|---|
| git branch | List local branches (* marks the current one) |
| git branch -a | List all branches (local + remote) |
| git branch name | Create a new branch from HEAD |
| git branch -d name | Delete a branch (safe: prevents unmerged deletion) |
| git branch -D name | Force-delete a branch (even if unmerged) |
| git switch name | Switch to an existing branch (Git 2.23+) |
| git switch -c name | Create and switch to a new branch |
| git checkout name | Switch to an existing branch (classic syntax) |
| git checkout -b name | Create and switch to a new branch (classic) |
| git merge name | Merge the named branch into the current branch |
| git merge --no-ff name | Merge with a merge commit even if fast-forward is possible |
| git rebase name | Reapply current branch commits on top of the named branch |
| git rebase -i HEAD~n | Interactive rebase: squash, reword, drop, or reorder last n commits |
| git cherry-pick <hash> | Apply a specific commit from another branch onto the current one |
Remote Operations
| Command | What it does |
|---|---|
| git clone <url> | Clone a remote repository into a new directory |
| git remote -v | List remote repositories and their URLs |
| git remote add origin <url> | Add a remote named "origin" |
| git fetch origin | Download objects and refs from a remote (does NOT merge) |
| git pull | Fetch + merge (shortcut: git fetch + git merge) |
| git pull --rebase | Fetch + rebase instead of merge (cleaner history) |
| git push | Push local commits to the upstream branch |
| git push -u origin branch | Push + set upstream tracking for the first time |
| git push --force-with-lease | Force push safely (refuses if remote has new commits) |
| git push origin --delete branch | Delete a remote branch |
Undoing Changes
| Command | What it does |
|---|---|
| git restore file | Discard unstaged changes in a file (Git 2.23+) |
| git restore --staged file | Unstage a file (keep changes in working tree) |
| git reset HEAD~1 | Undo the last commit, keep changes staged |
| git reset --soft HEAD~1 | Undo last commit, keep changes staged (same as above) |
| git reset --mixed HEAD~1 | Undo last commit, unstage changes (default mode) |
| git reset --hard HEAD~1 | Undo last commit AND discard changes entirely |
| git revert <hash> | Create a new commit that undoes the given commit (safe for public history) |
| git clean -fd | Remove untracked files and directories |
Stashing
| Command | What it does |
|---|---|
| git stash | Save uncommitted changes and revert to clean working tree |
| git stash push -m "name" | Stash with a descriptive message |
| git stash list | List all stashes |
| git stash pop | Apply the latest stash and remove it from the stash list |
| git stash apply | Apply the latest stash without removing it |
| git stash drop | Delete the latest stash |
| git stash clear | Delete all stashes |
Inspection & History
| Command | What it does |
|---|---|
| git log | Show commit history (most recent first) |
| git log --oneline --graph | Compact log with ASCII graph of branches |
| git log --oneline -n 5 | Show only the last 5 commits |
| git log --author="name" | Filter commits by author |
| git log --grep="fix" | Search commit messages for "fix" |
| git show <hash> | Show the details of a specific commit (diff + metadata) |
| git blame file | Show who last modified each line of a file |
| git shortlog -sn | Summary of commits per author |
| git tag | List all tags |
| git tag v1.0.0 | Create a lightweight tag at HEAD |
| git describe --tags | Show the nearest tag relative to HEAD |
Typical Day Workflow
git switch -c feature/loginCreate a new feature branch from main
git add . && git commit -m "add login form"Make changes, stage, and commit on the feature branch
git push -u origin feature/loginPush the feature branch to GitHub/GitLab
git switch main && git pullSwitch back to main and get the latest changes
git merge feature/loginMerge the completed feature into main
git pushPush the updated main to the remote
Generate a .gitignore for your project →
Choose your tech stack and get a ready-to-use .gitignore file instantly.