Essential Git commands for everyday development. Covers setup, branching, merging, rebasing, remote operations, stashing, undoing changes, and viewing history. A must-have reference for any developer using Git.
| Code / Syntax | Description |
|---|---|
git init | Initialize a new Git repository |
git clone <url> | Clone a remote repository |
git config --global user.name "Name" | Set your global username |
git config --global user.email "email" | Set your global email |
git config --list | List all Git configuration |
git config --global core.editor "code --wait" | Set VS Code as default editor |
git config --global init.defaultBranch main | Set default branch name to main |
git config --global alias.co checkout | Create a shortcut alias |
| Code / Syntax | Description |
|---|---|
git status | Show working tree status |
git add <file> | Stage a specific file |
git add . | Stage all changes in current directory |
git commit -m "message" | Commit staged changes with a message |
git commit -am "message" | Stage tracked files and commit in one step |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git rm <file> | Remove file and stage deletion |
git mv <old> <new> | Rename/move a file and stage it |
| Code / Syntax | Description |
|---|---|
git branch | List local branches |
git branch -a | List all branches (local + remote) |
git branch <name> | Create a new branch |
git checkout <branch> | Switch to a branch |
git checkout -b <branch> | Create and switch to a new branch |
git switch <branch> | Switch branches (modern syntax) |
git switch -c <branch> | Create and switch (modern syntax) |
git branch -d <branch> | Delete a merged branch |
git branch -D <branch> | Force-delete an unmerged branch |
git branch -m <old> <new> | Rename a branch |
| Code / Syntax | Description |
|---|---|
git merge <branch> | Merge branch into current branch |
git merge --no-ff <branch> | Merge with a merge commit (no fast-forward) |
git merge --squash <branch> | Squash all commits into one before merge |
git merge --abort | Abort a conflicted merge |
git rebase <branch> | Rebase current branch onto another |
git rebase --abort | Abort an in-progress rebase |
git rebase --continue | Continue rebase after resolving conflicts |
git cherry-pick <commit> | Apply a specific commit to current branch |
| Code / Syntax | Description |
|---|---|
git remote -v | List remote repositories |
git remote add origin <url> | Add a remote named origin |
git push -u origin <branch> | Push branch and set upstream |
git push | Push commits to tracked remote branch |
git pull | Fetch and merge remote changes |
git pull --rebase | Fetch and rebase instead of merge |
git fetch | Download remote changes without merging |
git fetch --prune | Fetch and remove deleted remote branches |
git push origin --delete <branch> | Delete a remote branch |
| Code / Syntax | Description |
|---|---|
git restore <file> | Discard changes in working directory |
git restore --staged <file> | Unstage a file (keep changes) |
git reset HEAD~1 | Undo last commit, keep changes staged |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --hard HEAD~1 | Undo last commit and discard changes |
git revert <commit> | Create a new commit that undoes a commit |
git clean -fd | Remove untracked files and directories |
git checkout -- <file> | Restore file to last committed state |
| Code / Syntax | Description |
|---|---|
git stash | Stash current changes |
git stash push -m "message" | Stash with a descriptive message |
git stash list | List all stashes |
git stash pop | Apply and remove the latest stash |
git stash apply | Apply the latest stash (keep in list) |
git stash drop | Remove the latest stash |
git stash clear | Remove all stashes |
git stash show -p | Show stash diff |
| Code / Syntax | Description |
|---|---|
git log | Show commit history |
git log --oneline | Compact commit history (one line each) |
git log --graph --oneline --all | Visual branch graph |
git log -p <file> | Show changes over time for a file |
git log --author="name" | Filter commits by author |
git log --since="2024-01-01" | Commits after a date |
git blame <file> | Show who changed each line |
git show <commit> | Show details of a specific commit |
git shortlog -sn | Commit count per author |
Found this cheat sheet useful? Check out our other references and tools.