A one-page reference for the Git commands I use most, and the ones my students ask about. It starts with setup and goes through staging, branches, remotes, and fixing mistakes.
Install Git
- GitHub Desktop for Windows and macOS, if you prefer a graphical app
- Git for the command line for Windows, macOS, and Linux
Key terms
](/assets/img/blog/2024_12_github_cheatsheet_github_flow.png)
- Git: the version control tool that runs on your computer.
- GitHub: a website that hosts Git repositories and adds collaboration tools.
- Commit: a saved snapshot of your project.
- Branch: a separate line of work, so you can try changes without touching
main. - Clone: a full local copy of a repository, including its history.
- Remote: a copy of the repository on a server, usually called
origin. - Fork: your own copy of someone else’s repository on GitHub.
- Pull request: a request to merge your branch, with a place for review and discussion.
- HEAD: the commit you currently have checked out.
First-time setup
git config --global user.name "[Your Name]" # Name shown on your commits
git config --global user.email "[you@email.com]" # Email shown on your commits
git config --global color.ui auto # Colored output
Start a repository
git init # Turn the current folder into a Git repository
git clone [repository-url] # Download a repository with its full history
Stage and commit changes
git status # What's modified, staged, and untracked
git add [file] # Stage a file for the next commit
git reset [file] # Unstage a file (keeps your edits)
git diff # Changes you haven't staged yet
git diff --staged # Changes staged for the next commit
git commit -m "[message]" # Commit what's staged
git add -A vs git add . vs git add -u vs git commit -a
These four look similar but stage different things. The key question is whether untracked (new) files and deletions are included.
| Command | New (untracked) files | Modified files | Deleted files | Scope |
|---|---|---|---|---|
git add -A |
Yes | Yes | Yes | Whole repository |
git add . |
Yes | Yes | Yes | Current directory and below |
git add -u |
No | Yes | Yes | Whole repository |
git commit -a |
No | Yes | Yes | Whole repository (stages and commits in one step) |
git add -A # Stage everything: new, modified, and deleted files
git add -u # Stage modified and deleted tracked files only (no new files)
git commit -a -m "msg" # Auto-stage modified/deleted TRACKED files, then commit
git add new_file.py && git commit -a -m "msg" # New files must be added first
Common gotcha: git commit -a does not commit untracked files. A file you just created is ignored until you run git add on it once. After that, Git tracks it and git commit -a picks up its future changes.
Branches
git branch # List local branches (* marks the current one)
git branch -a # List local AND remote-tracking branches (e.g. remotes/origin/main)
git branch -r # List remote-tracking branches only
git branch [name] # Create a branch
git checkout [name] # Switch to a branch (newer Git: git switch [name])
git merge [branch] # Merge another branch into the current one
git log # Commit history
Inspect history
git log # Full commit history
git log branchB..branchA # Commits in branchA that aren't in branchB
git log --follow [file] # History of one file, including renames
git diff branchB...branchA # Changes on branchA since it split from branchB
git show [commit-SHA] # Details and changes of one commit
Work with remotes
git remote add [alias] [url] # Add a remote (usually "origin")
git fetch [alias] # Download new commits without merging
git merge [alias]/[branch] # Merge a fetched remote branch
git push [alias] [branch] # Upload your commits
git pull # Fetch and merge in one step
Move and delete files
git rm [file] # Delete a file and stage the deletion
git mv [old-path] [new-path] # Move or rename a file and stage it
git log --stat -M # History showing renamed and moved files
Rewrite history and save work for later
git rebase [branch] # Replay your commits on top of another branch
git reset --hard [commit] # Go back to a commit and DISCARD everything after it
git reset --hard deletes uncommitted work permanently. Use it carefully.
git stash # Put your uncommitted changes aside
git stash list # See saved stashes
git stash pop # Bring back the latest stash
Ignore files
List files and folders Git should ignore in a .gitignore file at the root of your repository:
logs/
*.notes
pattern*/
To ignore files in every repository on your computer:
git config --global core.excludesfile [path-to-ignore-file]
The six commands you’ll use every day
- git clone: get a copy of a repository
- git status: see what changed
- git add: stage changes
- git commit: save a snapshot
- git push: upload your commits
- git pull: download others’ commits
Good habits
- Write clear commit messages that say what changed and why.
- Do new work on a branch, not directly on
main, and merge through a pull request. - Automate checks with GitHub Actions so tests run on every push.