← All posts

Getting comfortable with Git branches

8 February 2023 · 1 min read
#git#devops

Git felt scary until I understood one thing: a branch is just a label pointing at a commit. Creating one is cheap, and you can always get back.

The everyday flow I use:

  1. git switch main and git pull to start from the latest.
  2. git switch -c fix-login-timeout to make a branch for the change.
  3. Small commits with clear messages: what changed and why.
  4. git push -u origin fix-login-timeout, then open a pull request.
  5. After it's merged, delete the branch and go back to main.

Commands that save me when things go wrong:

  • git status: always the first thing to run. It tells you where you are.
  • git log --oneline --graph -10: see the shape of recent history.
  • git restore file: throw away changes to one file.
  • git stash: park unfinished work while you switch branches.

And one rule: never rewrite history on a branch other people are using.

T
Written by Teza Wynn Htut

Cloud and infrastructure engineer in Auckland. I write down the fixes and lessons from things I build, and make tutorial videos on YouTube.

Watch on YouTube