env.dev

Git Cheat Sheet

Essential Git commands organized by workflow: setup, branching, staging, history, remotes, and undoing changes.

A quick reference for everyday Git commands. Organized by workflow so you can find what you need fast.

Configuration

CommandDescription
git config --global user.name "Name"Set your name
git config --global user.email "email"Set your email
git config --global core.editor vimSet default editor
git config --listShow all settings

Creating Repositories

CommandDescription
git initCreate a new local repo
git clone <url>Clone a remote repo
git clone --depth 1 <url>Shallow clone (latest commit only)

Staging & Committing

CommandDescription
git statusShow working tree status
git add <file>Stage a file
git add -pStage changes interactively (by hunk)
git commit -m "msg"Commit with message
git commit --amendAmend the last commit
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching

CommandDescription
git branchList local branches
git branch <name>Create a new branch
git switch <name>Switch to a branch
git switch -c <name>Create and switch to a new branch
git merge <branch>Merge branch into current
git rebase <branch>Rebase current onto branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force delete a branch

Remote Repositories

CommandDescription
git remote -vList remotes
git remote add origin <url>Add a remote
git fetchDownload remote changes
git pullFetch and merge
git pull --rebaseFetch and rebase
git pushPush to remote
git push -u origin <branch>Push and set upstream

Viewing History

CommandDescription
git log --onelineCompact commit log
git log --graph --allVisual branch graph
git log -p <file>Show changes to a file over time
git show <commit>Show a specific commit
git blame <file>Show who changed each line
git reflogShow history of HEAD changes

Undoing Changes

CommandDescription
git restore <file>Discard working directory changes
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit (keep changes staged)
git reset --mixed HEAD~1Undo last commit (keep changes unstaged)
git reset --hard HEAD~1Undo last commit (discard changes)
git revert <commit>Create a new commit that undoes a commit

Stashing

CommandDescription
git stashStash working changes
git stash popRestore and remove latest stash
git stash listList all stashes
git stash dropRemove the latest stash
git stash applyRestore latest stash (keep in stash list)

Tags

CommandDescription
git tag <name>Create a lightweight tag
git tag -a <name> -m "msg"Create an annotated tag
git tagList all tags
git push origin <tag>Push a tag to remote
git push origin --tagsPush all tags