Git Cheatsheet
Common Git commands for daily development workflow.
Configuration
| Command | Description |
|---|---|
| git config --global user.name "Name" | Set your name globally |
| git config --global user.email "email" | Set your email globally |
| git config --global alias.<name> <command> | Create a Git alias |
| git config --list | List all configuration settings |
Getting Started
| Command | Description |
|---|---|
| git init | Initialize a new Git repository |
| git clone <url> | Clone an existing repository |
| git clone <url> <dir> | Clone into a specific directory |
Basic Workflow
| Command | Description |
|---|---|
| git status | Show working tree status |
| git add <file> | Stage a file |
| git add . | Stage all changes |
| git commit -m "message" | Commit staged changes |
| git commit -am "message" | Add and commit in one step (tracked files) |
| git rm <file> | Remove a file from tracking |
| git mv <old> <new> | Rename a tracked file |
Branching & Merging
| Command | Description |
|---|---|
| git branch | List local branches |
| git branch <name> | Create a new branch |
| git checkout <branch> | Switch to a branch |
| git checkout -b <name> | Create and switch to a new branch |
| git merge <branch> | Merge a branch into the current branch |
| git rebase <branch> | Rebase current branch onto another |
| git branch -d <name> | Delete a branch (safe) |
| git branch -D <name> | Force delete a branch |
Remote Repositories
| Command | Description |
|---|---|
| git remote -v | List remote repositories |
| git remote add <name> <url> | Add a remote |
| git push <remote> <branch> | Push commits to a remote |
| git push -u <remote> <branch> | Push and set upstream |
| git pull | Fetch and merge from upstream |
| git fetch | Fetch changes without merging |
| git remote remove <name> | Remove a remote |
Inspection & History
| Command | Description |
|---|---|
| git log | Show commit history |
| git log --oneline | Compact commit history |
| git log --graph | Show commit history as a graph |
| git diff | Show unstaged changes |
| git diff --staged | Show staged changes |
| git show <commit> | Show details of a specific commit |
| git blame <file> | Show who changed each line |
Undoing Changes
| Command | Description |
|---|---|
| git checkout -- <file> | Discard unstaged changes |
| git reset <file> | Unstage a file |
| git reset --soft HEAD~1 | Undo last commit, keep changes staged |
| git reset --hard HEAD~1 | Undo last commit, discard changes |
| git revert <commit> | Create a new commit that undoes a previous one |
| git stash | Stash working directory changes |
| git stash pop | Apply and remove the latest stash |
| git stash list | List stashes |
Tagging
| Command | Description |
|---|---|
| git tag | List tags |
| git tag <name> | Create a lightweight tag |
| git tag -a <name> -m "msg" | Create an annotated tag |
| git push --tags | Push tags to remote |