Git & GitHub CLI (gh) Cheatsheet


Installation

Git

  • Linux
sudo apt update && sudo apt install git   # Debian/Ubuntu
sudo dnf install git                      # Fedora
sudo yum install git                      # CentOS/RHEL
  • macOS
brew install git
  • Windows
  1. Download the installer from https://git-scm.com/download/win
  2. Run the installer and follow the prompts

GitHub CLI (gh)

  • Linux
# Debian/Ubuntu
type -p curl >/dev/null || sudo apt install curl -y
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh
  • macOS
brew install gh
  • Windows
  1. Download the installer from https://github.com/cli/cli/releases/latest
  2. Or use winget:
winget install --id GitHub.cli

Configuration

  • Set your name and email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  • Set default branch name
git config --global init.defaultBranch main
  • Set up credential caching
git config --global credential.helper cache
  • View all config
git config --list

Creating & Cloning Repos

  • Initialize a new repo
git init
  • Clone a repo
git clone https://github.com/user/repo.git
  • Create a new repo on GitHub (gh)
gh repo create myrepo --public --source=. --remote=origin

Basic Workflow

  • Check repo status
git status
  • Add files to staging
git add <file>
git add .
  • Commit changes
git commit -m "Commit message"
  • View commit history
git log --oneline --graph --decorate --all
  • Push to remote
git push origin main
  • Pull from remote
git pull

Branching & Merging

  • List branches
git branch
  • Create new branch (classic)
git checkout -b feature/my-feature
  • Create new branch (modern)
git switch -c feature/my-feature
  • Switch branches (classic)
git checkout main
  • Switch branches (modern)
git switch main
  • Merge branch
git merge feature/my-feature
  • Delete branch
git branch -d feature/my-feature
  • View branch graph
git log --oneline --graph --all

Stashing & Cleaning

  • Stash changes
git stash
  • List stashes
git stash list
  • Apply latest stash
git stash apply
  • Drop latest stash
git stash drop
  • Clean untracked files
git clean -fd

Remote Repositories

  • Add remote
git remote add origin https://github.com/user/repo.git
  • Show remotes
git remote -v
  • Change remote URL
git remote set-url origin https://github.com/user/new-repo.git

Tagging & Releases

  • Create a tag
git tag v1.0.0
  • Push tags
git push origin --tags
  • List tags
git tag
  • Create a GitHub release (gh)
gh release create v1.0.0 --title "v1.0.0" --notes "First release"

Collaboration & Pull Requests

  • Fetch all branches
git fetch --all
  • Rebase your branch
git pull --rebase origin main
  • Create a pull request (gh)
gh pr create --base main --head feature/my-feature --title "Add feature" --body "Description"
  • List pull requests (gh)
gh pr list
  • Checkout a pull request (gh)
gh pr checkout <number>

Troubleshooting

  • See what changed in working directory
git status
git diff
  • Undo last commit (keep changes staged)
git reset --soft HEAD~1
  • Undo last commit (discard changes)
git reset --hard HEAD~1
  • Restore a deleted file
git checkout HEAD -- <file>
  • Fix merge conflicts
git status
# Edit conflicted files, then:
git add <file>
git commit
  • Remove file from staging
git reset <file>
  • Remove file from repo but keep locally
git rm --cached <file>
  • Set upstream for a branch
git push --set-upstream origin my-branch
  • See which branch tracks which remote
git branch -vv
  • See which files are ignored
git check-ignore -v *

Useful Aliases

  • Shorter log
git config --global alias.lg "log --oneline --graph --decorate --all"
  • Quick add & commit
git config --global alias.ac '!git add -A && git commit -m'

Resources