Git and GitHub Cheat Sheet

This cheat sheet covers every important Git and GitHub command organized by category. Keep this page bookmarked as a go-to reference during development.

Setup and Configuration

CommandDescription
git --versionCheck installed Git version
git config --global user.name "Name"Set global username
git config --global user.email "email"Set global email
git config --global core.editor "code --wait"Set VS Code as default editor
git config --global init.defaultBranch mainSet default branch name to main
git config --listShow all configuration settings
git config --global --editOpen global config file in editor

Starting a Repository

CommandDescription
git initInitialize a new Git repo in current folder
git init folder-nameCreate a new folder and initialize Git in it
git clone <url>Clone a remote repository locally
git clone <url> folder-nameClone into a specific folder name
git clone --depth 1 <url>Shallow clone (latest commit only)
git clone --recurse-submodules <url>Clone with all submodules

Working with Files — Stage and Commit

CommandDescription
git statusShow working directory and staging area status
git status -sCompact status output
git add <file>Stage a specific file
git add .Stage all changes in current directory
git add -AStage all changes across the entire repo
git add -pInteractively stage parts of a file
git commit -m "message"Commit staged changes with a message
git commit -am "message"Stage tracked files and commit in one step
git commit --amend -m "new message"Fix the last commit message
git commit --amend --no-editAdd staged changes to last commit without changing message

Viewing History

CommandDescription
git logFull commit history
git log --onelineCompact one-line per commit
git log --oneline --graph --allVisual branch graph of all commits
git log -5Last 5 commits
git log -pShow changes in each commit
git log --statFiles changed per commit
git log --author="Name"Commits by a specific author
git log --grep="keyword"Search commits by message keyword
git log -- <file>Commits that touched a specific file
git show <hash>Show details of a specific commit
git blame <file>Show who last changed each line
git blame -L 10,25 <file>Blame for specific lines only
git reflogShow all HEAD movements (recovery tool)

Comparing Changes

CommandDescription
git diffUnstaged changes vs last commit
git diff --stagedStaged changes vs last commit
git diff HEADAll changes vs last commit
git diff branch1 branch2Compare two branches
git diff hash1 hash2Compare two commits
git diff --name-onlyShow only changed file names
git diff --statSummary of changes per file

Branching

CommandDescription
git branchList local branches
git branch -aList all branches (local + remote)
git branch <name>Create a new branch
git switch <branch>Switch to an existing branch
git switch -c <branch>Create and switch to new branch
git switch -Switch to the previous branch
git checkout <branch>Switch branch (older syntax)
git checkout -b <branch>Create and switch (older syntax)
git branch -m <new-name>Rename current branch
git branch -d <branch>Delete a merged branch
git branch -D <branch>Force-delete an unmerged branch
git push origin --delete <branch>Delete a remote branch

Merging and Rebasing

CommandDescription
git merge <branch>Merge a branch into the current branch
git merge --no-ff <branch>Merge with a merge commit (no fast-forward)
git merge --squash <branch>Squash branch commits into one before merging
git merge --abortCancel a merge in progress
git rebase <branch>Rebase current branch onto another
git rebase -i HEAD~3Interactive rebase of last 3 commits
git rebase --continueContinue after resolving rebase conflict
git rebase --abortCancel a rebase in progress
git cherry-pick <hash>Apply a specific commit to current branch
git cherry-pick <hash> --no-commitApply changes without committing

Undoing Changes

CommandDescription
git restore <file>Discard unstaged changes in a file
git restore --staged <file>Unstage a file (keep changes)
git reset HEAD~1Undo last commit, keep changes unstaged
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard HEAD~1Undo last commit and discard all changes
git reset --hard origin/mainReset to the remote branch state
git revert <hash>Create a new commit that undoes a specific commit
git revert <hash> --no-editRevert without opening the editor

Stashing

CommandDescription
git stashSave current changes to the stash
git stash save "message"Stash with a descriptive label
git stash -uStash including untracked files
git stash listList all stashes
git stash popApply and remove the latest stash
git stash applyApply the latest stash (keep it in list)
git stash apply stash@{2}Apply a specific stash
git stash drop stash@{1}Delete a specific stash
git stash clearDelete all stashes
git stash show -pShow full diff of latest stash

Remote Repositories

CommandDescription
git remoteList all remotes
git remote -vList remotes with their URLs
git remote add origin <url>Add a remote named origin
git remote set-url origin <url>Change a remote URL
git remote rename origin upstreamRename a remote
git remote remove originRemove a remote
git push origin <branch>Push a branch to the remote
git push -u origin mainPush and set tracking (first push)
git pushPush current branch (after tracking set)
git push --forceForce push (dangerous on shared branches)
git push origin --tagsPush all tags to remote
git pullFetch and merge from remote
git pull --rebasePull and rebase instead of merge
git fetch originDownload remote changes without merging

Tags

CommandDescription
git tagList all tags
git tag v1.0.0Create a lightweight tag
git tag -a v1.0.0 -m "message"Create an annotated tag
git tag -a v1.0.0 <hash> -m "msg"Tag a specific past commit
git show v1.0.0Show tag details
git push origin v1.0.0Push a specific tag to remote
git push origin --tagsPush all local tags
git tag -d v1.0.0Delete a local tag
git push origin --delete v1.0.0Delete a remote tag

Debugging and Finding Issues

CommandDescription
git bisect startStart a binary search for a bad commit
git bisect good <hash>Mark a commit as good
git bisect badMark the current commit as bad
git bisect resetEnd the bisect session
git bisect run <script>Automate bisect with a test script
git reflogShow full history of HEAD movements
git check-ignore -v <file>Show which .gitignore rule is ignoring a file

Advanced Commands

CommandDescription
git worktree add <path> <branch>Check out a branch in a separate folder
git worktree listList all active worktrees
git worktree remove <path>Remove a worktree
git submodule add <url> <path>Add a submodule
git submodule update --initInitialize and update submodules
git submodule update --remoteUpdate submodules to latest remote commit
git gcClean up and optimize the repository
git fsckCheck integrity of the repository
git cat-file -p <hash>Show the content of any Git object
git rev-parse HEADShow the full hash of the current commit

GitHub-Specific Actions

ActionDescription
Fork a repositoryClick "Fork" on any GitHub repo page to create a personal copy
Open a Pull RequestPush a branch → Click "Compare and pull request" on GitHub
Create an IssueIssues tab → New issue → Fill in title and description
Close issue via commitAdd "Fixes #42" in commit message or PR description
Add SSH key to GitHubSettings → SSH and GPG keys → New SSH key
Protect main branchSettings → Branches → Add rule → Require PRs
Create a ReleaseReleases → Draft new release → Choose tag → Publish
Enable GitHub PagesSettings → Pages → Choose source branch
Add GitHub ActionsCreate .github/workflows/name.yml with workflow config

Common Symbols and Notation

SymbolMeaning
HEADThe current commit / current position in history
HEAD~1One commit before HEAD
HEAD~3Three commits before HEAD
HEAD^The parent commit of HEAD (same as HEAD~1)
originThe default name for the remote repository
origin/mainThe main branch on the remote (origin)
@{u}The upstream (tracking) branch of the current branch
stash@{0}The most recent stash entry

Summary

This cheat sheet covers the complete range of Git commands — from daily essentials like git add, git commit, and git push, to advanced tools like git bisect, git worktree, and git reflog. Bookmark this page and return to it whenever a command needs a quick reminder. The more Git is used, the more natural these commands become — soon they will feel like second nature.

Leave a Comment

Your email address will not be published. Required fields are marked *