Git & GitHub CLI (gh) Cheatsheet
Installation
Git
sudo apt update && sudo apt install git # Debian/Ubuntu
sudo dnf install git # Fedora
sudo yum install git # CentOS/RHEL
brew install git
- Download the installer from https://git-scm.com/download/win
- Run the installer and follow the prompts
GitHub CLI (gh
)
# 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
brew install gh
- Download the installer from https://github.com/cli/cli/releases/latest
- Or use winget:
winget install --id GitHub.cli
Configuration
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
- Set up credential caching
git config --global credential.helper cache
git config --list
Creating & Cloning Repos
git init
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
git status
git add <file>
git add .
git commit -m "Commit message"
git log --oneline --graph --decorate --all
git push origin main
git pull
Branching & Merging
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
git switch main
git merge feature/my-feature
git branch -d feature/my-feature
git log --oneline --graph --all
Stashing & Cleaning
git stash
git stash list
git stash apply
git stash drop
git clean -fd
Remote Repositories
git remote add origin https://github.com/user/repo.git
git remote -v
git remote set-url origin https://github.com/user/new-repo.git
Tagging & Releases
git tag v1.0.0
git push origin --tags
git tag
- Create a GitHub release (gh)
gh release create v1.0.0 --title "v1.0.0" --notes "First release"
Collaboration & Pull Requests
git fetch --all
git pull --rebase origin main
- Create a pull request (gh)
gh pr create --base main --head feature/my-feature --title "Add feature" --body "Description"
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
git checkout HEAD -- <file>
git status
# Edit conflicted files, then:
git add <file>
git commit
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
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.ac '!git add -A && git commit -m'
Resources