Git Cheat Sheet¶
Keep this page open in a tab while you work.
The mental model¶
Working dir --add--> Staging --commit--> Repository --push--> GitHub
(edit) (ready) (saved locally) (shared)
<----------------- pull ---------------------------
One-time setup¶
See the Setup page for the full walkthrough (install + account + SSH).
git --version # check git is installed
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# SSH key: connect this laptop to GitHub (do once)
ssh-keygen -t ed25519 -C "you@example.com"
ssh -T git@github.com # test — greets you by username
Start a repo¶
The daily loop ⭐ memorize this¶
git status # what's going on? (run this constantly)
git pull # get teammates' latest work
# ...edit files...
git add <file> # or git add .
git commit -m "Clear message about WHY"
git push # share your work
Shortcut once you're comfortable: git commit -am
git commit -am "msg" stages and commits in one step — but only for files git
already tracks. It skips new/untracked files, so stick with git add → git
commit whenever a file is new.
Staging: what git add picks up¶
git add fit.py # one specific file (safest)
git add . # everything under the current folder
git add -A # = git add --all # everything in the whole repo, wherever you are
git add -u # only already-tracked files that changed (no new files)
From the repo's top folder, git add . and git add -A behave the same; they differ only
inside a subfolder (. = this folder, -A = whole repo). When unsure, add by name and check
git status.
Look around¶
git status # changed / staged / untracked files
git log --oneline # compact history
git diff # unstaged changes, line by line
git diff --staged # what you're about to commit
Undo¶
git restore <file> # discard uncommitted changes to a file
git restore --staged <file> # unstage (keep the edit)
git commit --amend -m "New msg" # fix the last commit message
Branches (parallel work)¶
git switch -c my-idea # create + switch to a branch
git switch main # go back to main
git branch # list branches
git branch -d my-idea # delete a merged branch
git merge my-idea # bring my-idea's work into current branch (solo)
Older equivalents you'll see elsewhere (checkout predates switch):
git branch my-idea # create only (stay put), then...
git checkout my-idea # ...switch onto it
git checkout -b my-idea # create + switch, in one step
Pull Requests (the team way to merge)¶
git switch -c my-idea # work on a branch
# ...edit, commit...
git push -u origin my-idea # push the BRANCH, then open a PR on GitHub
# teammates review → Merge on GitHub → then locally:
git switch main && git pull # get the merged result
git branch -d my-idea # clean up
Merge conflicts & rebase¶
git pull --rebase # fold in teammates' work with a clean history
git rebase main # replay your branch on top of latest main
# conflict? edit the file, delete the <<<<<<< ======= >>>>>>> markers, then:
git add <file>
git rebase --continue
git rebase --abort # panic button: undo the whole rebase
merge vs rebase
merge = join branches with a merge commit (keeps exact history). rebase = replay
your commits on top of main (clean, straight-line history). Rebase only your own
un-pushed work; never rebase commits others already pulled.
Connect to GitHub¶
Two ways to authenticate — use whichever you set up in Setup:
# HTTPS — paste a Personal Access Token as the password on first push
git remote add origin https://github.com/you/repo.git
# SSH — passwordless once your key is added
git remote add origin git@github.com:you/repo.git
git remote -v # check which URL you're using
git push -u origin main # first push (sets the link)
git push # every push after that
HTTPS keeps asking for a password?
Turn on a credential helper so the token is remembered:
git config --global credential.helper osxkeychain (macOS) — Windows already has one.
Physics reminders¶
Data hygiene
- Never commit
.root, big.pdf, logs → put them in.gitignore. - Commit the code, not the plot. Plots are regenerated by running the code.
- Good message:
"Add fit range 5.20–5.30 GeV". Bad:"stuff". - Pull before you start, push when you pause.
.gitignore starter for analysis repos¶
*.root
*.log
*.pdf # remove this line if you DO want to track small figures
__pycache__/
*.pyc
.DS_Store
When something feels broken¶
git status almost always tells you what to do next. Read it before panicking. Nothing
committed is ever truly lost — ask a facilitator before running reset --hard.