Git Commands Cheat Sheet

Essential Git commands for everyday development. Bookmark this page for quick reference.

Setup & Configuration

CommandDescription
git initInitialize a new Git repository in the current directory
git clone <url>Clone a remote repository to your local machine
git config --global user.name "Name"Set your name for all repositories
git config --global user.email "email"Set your email for all repositories

Basic Workflow

CommandDescription
git statusShow the working tree status (modified, staged, untracked files)
git add <file>Stage a specific file for commit
git add .Stage all changes in the current directory
git commit -m "message"Create a commit with a message
git commit -am "message"Stage all tracked files and commit in one step
git diffShow unstaged changes
git diff --stagedShow staged changes (about to be committed)

Branching

CommandDescription
git branchList all 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 switch <branch>Switch branches (newer Git alternative to checkout)
git switch -c <name>Create and switch to a new branch
git branch -d <name>Delete a branch (safe — won't delete unmerged changes)
git branch -D <name>Force delete a branch (even if unmerged)

Merging & Rebasing

CommandDescription
git merge <branch>Merge a branch into the current branch
git merge --abortAbort a merge in progress
git rebase <branch>Rebase current branch onto another branch
git rebase --abortAbort a rebase in progress
git cherry-pick <commit>Apply a specific commit to the current branch

Remote Repositories

CommandDescription
git remote -vList all remote repositories
git remote add origin <url>Add a remote repository
git push origin <branch>Push a branch to the remote
git push -u origin <branch>Push and set upstream tracking
git pullFetch and merge changes from the remote
git fetchDownload changes from the remote without merging
git fetch --pruneFetch and remove deleted remote branches

Stashing

CommandDescription
git stashTemporarily save uncommitted changes
git stash popApply the most recent stash and remove it from the list
git stash listList all stashes
git stash dropDelete the most recent stash
git stash apply stash@{n}Apply a specific stash without removing it

History & Inspection

CommandDescription
git logShow commit history
git log --onelineShow compact commit history (one line per commit)
git log --graph --onelineShow commit history as a graph
git show <commit>Show details of a specific commit
git blame <file>Show who changed each line of a file
git reflogShow history of HEAD movements (useful for recovery)

Undoing Changes

CommandDescription
git restore <file>Discard changes in a working file
git restore --staged <file>Unstage a file (keep changes in working directory)
git reset HEAD~1Undo the last commit (keep changes staged)
git reset --hard HEAD~1Undo the last commit and discard all changes
git revert <commit>Create a new commit that undoes a previous commit
git clean -fdRemove untracked files and directories

Tags

CommandDescription
git tagList all tags
git tag v1.0.0Create a lightweight tag
git tag -a v1.0.0 -m "msg"Create an annotated tag with a message
git push --tagsPush all tags to the remote