
·3 min read
Getting Started with Git: A Practical Guide
The handful of Git commands you actually need, plus the habits I'd tell any newcomer on day one.
You don't need to know all of Git to be productive. You need about ten commands and a few habits that keep you from making a mess other people have to clean up. This is the short version I'd give someone joining the team.
Before the first commit
Create .gitignore first. Not after. Once a file is committed it's tracked, and adding it to .gitignore later does nothing until you untrack it:
git rm --cached .env # stop tracking, keep the file on diskA sane starting point for most repos: .env, *.log, node_modules/, __pycache__/, .venv/, .idea/, .vscode/, .DS_Store, *.tfstate. GitHub's github/gitignore repo has templates per language.
And set your identity once, or your commits show up as some random hostname:
git config --global user.name "Your Name"
git config --global user.email "you@company.com"The daily loop
git clone git@github.com:org/repo.git
git switch -c feature/short-description # new branch off where you are
git status # run this constantly
git add -p # stage hunk by hunk, review as you go
git commit -m "Fix retry logic in upload worker"
git push -u origin feature/short-descriptiongit add -p is the one I push hardest. git add . is how debug prints, half-finished files and the occasional .env end up in a PR. Going hunk by hunk forces you to read your own diff before anyone else does.
switch and restore, not checkout
git checkout does too many things: switches branches, creates branches, and throws away changes in files. Same command, very different blast radius. Since Git 2.23 it's split into:
git switch main # change branch
git switch -c new-branch # create and switch
git restore file.py # discard local changes in a file (gone for good)
git restore --staged file.py # unstage, keep the changesOld tutorials and Stack Overflow answers still use checkout. It works, but new people should learn the commands that say what they do.
Pull with rebase
A plain git pull merges, and on a shared branch that sprinkles "Merge branch 'main' of github.com..." commits all over the history. Set this once:
git config --global pull.rebase trueNow git pull replays your local commits on top of what's on the remote. If you hit conflicts mid-rebase, fix the files, git add them, then git rebase --continue. If it gets ugly, git rebase --abort puts you back where you started.
If you rebased a branch you already pushed, the push will be rejected. Use git push --force-with-lease, never plain --force. It refuses to overwrite commits on the remote that you haven't seen.
Never commit secrets
Tokens, passwords, kubeconfigs, service account JSON keys. Not in a "temporary" commit, not in a private repo, not "I'll remove it in the next commit". Deleting the file in a later commit doesn't remove it from history.
If it happens: rotate the secret first. Cleaning history (git filter-repo) comes second, because the moment it was pushed you have to assume someone or something already copied it. Turn on secret scanning / push protection in GitHub or GitLab so the push gets blocked before it lands.
Small habits
- Don't commit to
maindirectly. Branch, push, open a PR, even when you're alone on the repo. - Commit messages say what and why. "fix" and "wip" help nobody six months from now.
git log --oneline --graphwhen you're lost about where your branch sits.git branch -ddeletes merged branches only.-Ddeletes anyway, so double check before using it.
A note on how this was written: I used AI to help with phrasing and structure. The tone, the opinions, the war stories, the code, and everything else here are mine.