Git Cheat Sheet
Everyday Git commands.
You don't need to know all of Git to use it well. In day-to-day work, including on every client project here, these dozen commands cover almost everything. The rest you can look up the one time a year you need it.
If Git still feels scary, the Git & GitHub track walks through it slowly with a practice repository.
| Command | What it does |
|---|---|
git init |
New repo |
git clone URL |
Copy a repo |
git status |
What changed |
git add -A |
Stage everything |
git commit -m "msg" |
Save a snapshot |
git switch -c name |
New branch |
git merge name |
Merge branch |
git pull / git push |
Sync with remote |
git log --oneline --graph |
History |
git stash / git stash pop |
Shelve changes |
git restore file |
Discard changes |
git revert <sha> |
Undo a commit safely |
A normal day with Git
git switch main
git pull # get everyone else's changes
git switch -c fix-invoice-pdf # new branch for your work
# ...edit files...
git status # see what changed
git add -A
git commit -m "Fix GST total on invoice PDF"
git push -u origin fix-invoice-pdf
Then open a pull request on GitHub, get it reviewed, and merge.
Undoing things, safely
This is where people panic, so here is the plain version:
- Changed a file and want the old version back (not committed yet):
git restore file.py. - Staged something by mistake:
git restore --staged file.py. The change stays in your file, it's just unstaged. - Committed but not pushed, want to change the message or add a file:
git commit --amend. - Already pushed a bad commit:
git revert <sha>. It adds a new commit that undoes the old one, so nobody else's history breaks.
Avoid git reset --hard and git push --force on shared branches unless you're very sure. They rewrite history, and that's how people lose work.
Good habits
- Commit small and often, with messages that say why, like "Fix rounding in GST total", not "changes".
- Never commit
.envfiles, passwords or API keys. Add them to.gitignorebefore your first commit. If a key does get pushed, treat it as leaked and generate a new one. Deleting the file afterwards doesn't remove it from history. git stashis handy when you need to switch branches quickly but aren't ready to commit.