A practical quick reference for the most common repository tasks.
Assumptions: The default branch is
main, the remote is calledorigin, and GitHub CLI (gh) is optional. Replace placeholders such asOWNER,REPO, andBRANCH_NAME.
# HTTPS
git clone https://github.com/OWNER/REPO.git
# SSH
git clone git@github.com:OWNER/REPO.git
cd REPO
Check the configured remote:
git remote -v
git status
Always start from an up-to-date default branch:
git switch main
git pull --ff-only origin main
git switch --create feature/short-description
Common branch prefixes:
feature/ — new functionalityfix/ — bug fixdocs/ — documentationrefactor/ — code restructuringtest/ — testschore/ — maintenanceOlder Git versions can use git checkout -b BRANCH_NAME instead of git switch --create BRANCH_NAME.
# Working-tree summary
git status
# Unstaged changes
git diff
# Staged changes
git diff --cached
# List local and remote branches
git branch --all
Edit the files, run the relevant tests, then review and commit only the intended changes:
git add path/to/file path/to/another-file
# Or stage all tracked and untracked changes after reviewing them:
# git add .
git diff --cached
git commit -m "feat: describe the change"
git push --set-upstream origin HEAD
Useful commit prefixes:
feat: add a new capability
fix: correct an error
docs: update documentation
test: add or update tests
refactor: simplify implementation
chore: update tooling
Install and authenticate gh first, then run:
gh pr create \
--base main \
--head "$(git branch --show-current)" \
--title "feat: describe the change" \
--body "## Summary
- Explain what changed
## Testing
- Explain what you tested"
Create a draft PR:
gh pr create --draft --base main --title "WIP: describe the change"
main as the base branch and your feature branch as the compare branch.A PR updates automatically when new commits are pushed to its branch:
git switch feature/short-description
# edit files and run tests
git add .
git commit -m "fix: address review feedback"
git push
View the PR and checks with gh:
gh pr view --web
gh pr checks
Update the local default branch, then rebase your feature branch:
git fetch origin
git switch main
git pull --ff-only origin main
git switch feature/short-description
git rebase main
git push --force-with-lease
Use --force-with-lease, not plain --force, because it protects against overwriting someone else's newer remote work.
After the PR is merged or abandoned:
# Delete the local branch; -d refuses if it is not merged
git switch main
git branch --delete feature/short-description
# Force-delete a local branch only when you are certain it is no longer needed
git branch --delete --force feature/short-description
# Delete the remote branch
git push origin --delete feature/short-description
# Remove stale remote-tracking references
git fetch --prune
With GitHub CLI:
gh pr close PR_NUMBER
gh pr delete-branch PR_NUMBER
# Download remote updates without changing local files
git fetch origin
# Fetch and integrate the current branch
git pull --ff-only
# Push the current branch
git push
Prefer git pull --ff-only when you want Git to stop instead of creating an unexpected merge commit.
# Unstage a file, keeping its changes
git restore --staged path/to/file
# Discard local changes in a file — destructive
git restore path/to/file
# Amend the last commit before it has been shared
git commit --amend
# Undo a published commit safely by creating a new commit
git revert COMMIT_SHA
Do not use
git reset --hardor rewrite shared history unless you understand the impact and have authorization.
Before merging:
With GitHub CLI:
gh pr checks PR_NUMBER
gh pr merge PR_NUMBER --squash --delete-branch
The exact merge method depends on repository policy. Common choices are merge commit, squash, and rebase.
git clone https://github.com/OWNER/REPO.git
cd REPO
git switch main
git pull --ff-only origin main
git switch --create feature/my-change
# Make and test changes
git add .
git diff --cached
git commit -m "feat: implement my change"
git push --set-upstream origin HEAD
# Create the PR in GitHub or with:
gh pr create --base main --title "feat: implement my change"