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
| Command | Description |
|---|---|
git --version | Check 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 main | Set default branch name to main |
git config --list | Show all configuration settings |
git config --global --edit | Open global config file in editor |
Starting a Repository
| Command | Description |
|---|---|
git init | Initialize a new Git repo in current folder |
git init folder-name | Create a new folder and initialize Git in it |
git clone <url> | Clone a remote repository locally |
git clone <url> folder-name | Clone 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
| Command | Description |
|---|---|
git status | Show working directory and staging area status |
git status -s | Compact status output |
git add <file> | Stage a specific file |
git add . | Stage all changes in current directory |
git add -A | Stage all changes across the entire repo |
git add -p | Interactively 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-edit | Add staged changes to last commit without changing message |
Viewing History
| Command | Description |
|---|---|
git log | Full commit history |
git log --oneline | Compact one-line per commit |
git log --oneline --graph --all | Visual branch graph of all commits |
git log -5 | Last 5 commits |
git log -p | Show changes in each commit |
git log --stat | Files 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 reflog | Show all HEAD movements (recovery tool) |
Comparing Changes
| Command | Description |
|---|---|
git diff | Unstaged changes vs last commit |
git diff --staged | Staged changes vs last commit |
git diff HEAD | All changes vs last commit |
git diff branch1 branch2 | Compare two branches |
git diff hash1 hash2 | Compare two commits |
git diff --name-only | Show only changed file names |
git diff --stat | Summary of changes per file |
Branching
| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List 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
| Command | Description |
|---|---|
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 --abort | Cancel a merge in progress |
git rebase <branch> | Rebase current branch onto another |
git rebase -i HEAD~3 | Interactive rebase of last 3 commits |
git rebase --continue | Continue after resolving rebase conflict |
git rebase --abort | Cancel a rebase in progress |
git cherry-pick <hash> | Apply a specific commit to current branch |
git cherry-pick <hash> --no-commit | Apply changes without committing |
Undoing Changes
| Command | Description |
|---|---|
git restore <file> | Discard unstaged changes in a file |
git restore --staged <file> | Unstage a file (keep changes) |
git reset HEAD~1 | Undo last commit, keep changes unstaged |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --hard HEAD~1 | Undo last commit and discard all changes |
git reset --hard origin/main | Reset to the remote branch state |
git revert <hash> | Create a new commit that undoes a specific commit |
git revert <hash> --no-edit | Revert without opening the editor |
Stashing
| Command | Description |
|---|---|
git stash | Save current changes to the stash |
git stash save "message" | Stash with a descriptive label |
git stash -u | Stash including untracked files |
git stash list | List all stashes |
git stash pop | Apply and remove the latest stash |
git stash apply | Apply 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 clear | Delete all stashes |
git stash show -p | Show full diff of latest stash |
Remote Repositories
| Command | Description |
|---|---|
git remote | List all remotes |
git remote -v | List 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 upstream | Rename a remote |
git remote remove origin | Remove a remote |
git push origin <branch> | Push a branch to the remote |
git push -u origin main | Push and set tracking (first push) |
git push | Push current branch (after tracking set) |
git push --force | Force push (dangerous on shared branches) |
git push origin --tags | Push all tags to remote |
git pull | Fetch and merge from remote |
git pull --rebase | Pull and rebase instead of merge |
git fetch origin | Download remote changes without merging |
Tags
| Command | Description |
|---|---|
git tag | List all tags |
git tag v1.0.0 | Create 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.0 | Show tag details |
git push origin v1.0.0 | Push a specific tag to remote |
git push origin --tags | Push all local tags |
git tag -d v1.0.0 | Delete a local tag |
git push origin --delete v1.0.0 | Delete a remote tag |
Debugging and Finding Issues
| Command | Description |
|---|---|
git bisect start | Start a binary search for a bad commit |
git bisect good <hash> | Mark a commit as good |
git bisect bad | Mark the current commit as bad |
git bisect reset | End the bisect session |
git bisect run <script> | Automate bisect with a test script |
git reflog | Show full history of HEAD movements |
git check-ignore -v <file> | Show which .gitignore rule is ignoring a file |
Advanced Commands
| Command | Description |
|---|---|
git worktree add <path> <branch> | Check out a branch in a separate folder |
git worktree list | List all active worktrees |
git worktree remove <path> | Remove a worktree |
git submodule add <url> <path> | Add a submodule |
git submodule update --init | Initialize and update submodules |
git submodule update --remote | Update submodules to latest remote commit |
git gc | Clean up and optimize the repository |
git fsck | Check integrity of the repository |
git cat-file -p <hash> | Show the content of any Git object |
git rev-parse HEAD | Show the full hash of the current commit |
GitHub-Specific Actions
| Action | Description |
|---|---|
| Fork a repository | Click "Fork" on any GitHub repo page to create a personal copy |
| Open a Pull Request | Push a branch → Click "Compare and pull request" on GitHub |
| Create an Issue | Issues tab → New issue → Fill in title and description |
| Close issue via commit | Add "Fixes #42" in commit message or PR description |
| Add SSH key to GitHub | Settings → SSH and GPG keys → New SSH key |
| Protect main branch | Settings → Branches → Add rule → Require PRs |
| Create a Release | Releases → Draft new release → Choose tag → Publish |
| Enable GitHub Pages | Settings → Pages → Choose source branch |
| Add GitHub Actions | Create .github/workflows/name.yml with workflow config |
Common Symbols and Notation
| Symbol | Meaning |
|---|---|
HEAD | The current commit / current position in history |
HEAD~1 | One commit before HEAD |
HEAD~3 | Three commits before HEAD |
HEAD^ | The parent commit of HEAD (same as HEAD~1) |
origin | The default name for the remote repository |
origin/main | The 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.
