Git Commands Cheatsheet
A comprehensive reference for essential and advanced Git commands, from basic branching to rebase, cherry-pick, reflog disaster recovery, and GitHub Flow.
Basic Commands
git init # Initialize a new local Git repository
git clone <url> # Clone a remote repository to your machine
git status # Show working tree status (tracked/untracked files)
git diff # Show unstaged changes compared to the index
git diff --staged # Show staged changes compared to the last commit
git add <file> # Stage changes of a specific file
git add . # Stage all changes (new, modified, deleted)
git commit -m "message" # Commit staged changes with a descriptive message
git commit --amend # Modify the very last commit (change message or add files)
git log # Show commit history list
git log --oneline --graph # Visual, compact view of commit history and branches
git show <commit> # Show details and content changes of a specific commit
Branching
git branch # List local branches (* indicates current branch)
git branch -a # List both local and remote branches
git branch <name> # Create a new branch
git checkout <branch> # Switch to a specific branch
git checkout -b <name> # Create and switch to a new branch instantly
git switch <branch> # Modern, safer alternative to checkout for branch switching
git switch -c <name> # Modern alternative to create and switch to a new branch
git branch -d <name> # Delete a local branch (must be merged)
git branch -D <name> # Force delete a local branch (even if unmerged!)
git branch -m <new-name> # Rename the current branch
Merge
git merge <branch> # Merge target branch into the current branch
git merge --abort # Stop the merge and return to the pre-merge state (on conflict)
git merge --continue # Resume merging after resolving conflicts manually
git merge --squash <branch> # Combine all target branch commits into a single commit on current branch
Rebase
git rebase <branch> # Rebase current branch commits on top of target branch
git rebase -i HEAD~<n> # Interactive rebase of last 'n' commits (squash, reword, drop)
git rebase --abort # Cancel the rebase operation entirely
git rebase --continue # Resume rebasing after manually resolving conflicts
Cherry Pick
git cherry-pick <commit> # Copy a specific commit from another branch to current branch
git cherry-pick --abort # Cancel cherry-picking and undo local changes
git cherry-pick --continue # Resume cherry-picking after manual conflict resolution
Stash
git stash # Save uncommitted changes to a temporary stack and clean working tree
git stash -u # Stash changes including untracked files
git stash list # View all stashed change sets
git stash apply # Apply the most recent stash without removing it from stack
git stash apply stash@{n} # Apply a specific stash from list
git stash pop # Apply the most recent stash and delete it from stack
git stash drop stash@{n} # Delete a specific stash from stack
git stash clear # Delete all stashes from stack
Reset & Revert
# Revert (Safe: creates a new commit that undoes changes, preserves history)
git revert <commit> # Revert a specific commit by creating a new inverse commit
# Reset (Destructive: moves HEAD back, rewrites history, use carefully!)
git reset <commit> # Mixed reset (default): unstages changes, keeps files in working directory
git reset --soft <commit> # Soft reset: moves HEAD back, keeps changes staged in index
git reset --hard <commit> # Hard reset: moves HEAD back, DESTROYS all staged and unstaged changes!
git reset --hard HEAD~1 # Undo the very last commit and delete all its work
Reflog (Disaster Recovery)
# Git records every change to HEAD. Reflog lets you recover deleted branches or commits!
git reflog # List all movements of HEAD (even deleted commits/rebases)
git show HEAD@{n} # View changes at reflog index 'n'
git checkout HEAD@{n} # Checkout state at reflog index 'n'
git branch <name> HEAD@{n} # Re-create a deleted branch from a specific reflog state
git reset --hard HEAD@{n} # Force HEAD back to a lost commit/state found in reflog
GitHub Flow
# 1. Sync Fork / Local Main
git checkout main # Switch to main branch
git pull origin main # Pull latest changes from remote main
# 2. Create Feature Branch
git checkout -b feature/new # Create descriptive feature branch
# 3. Work & Commit
git add . && git commit -m "feat: implement new service" # Stage and commit work
# 4. Push Branch to Remote
git push -u origin feature/new # Push feature branch and set upstream tracking
# 5. Sync with Remote updates (rebase main)
git fetch origin # Fetch latest remote state
git rebase origin/main # Rebase feature commits on top of current remote main
# 6. Open Pull Request (PR)
# Open pull request on GitHub web or via GitHub CLI:
gh pr create --title "feat: new service" --body "Implements new service"