Skip to content
Engineering Bible
Git

Git on everyday work

A change passes through three places before it is shared

  • The working tree is the files you edit.
  • The staging area, also called the index, holds what the next commit will contain.
  • The local repository is the history in your .git folder. A remote is a copy elsewhere.
  1. Working treefiles you edit
  2. Staging areagit add copies changes here
  3. Local repositorygit commit saves a snapshot
  4. Remotegit push uploads commits
Nothing leaves your machine until you push.

Stage only what belongs in the commit

  • git add <file> stages the file as it is right now. Later edits need another git add.
  • git add -p walks through your changes and lets you stage them one piece at a time.
  • git diff shows what is not staged yet. git diff --staged shows what will be committed.
  • git status lists what is staged, changed and untracked. Run it often.

Small commits with clear messages pay off

  • One commit should make one logical change that could be undone on its own.
  • Start with a summary of 50 characters or less, then a blank line, then the why.
  • Write “Fix login redirect loop”, not “fixes” or “wip”.
  • git commit -S signs a commit with your GPG or SSH key, so GitHub can mark it Verified.

Each piece of work gets its own branch

  • git switch -c fix-login creates a branch at the current commit and moves you onto it.
  • git switch main goes back. git switch - returns to the branch you were on before.
  • git switch and git restore each do one of the jobs that git checkout does alone.

A remote is another copy of the repository

  • git clone downloads the full history and names the place it came from origin.
  • origin/main is your record of where main was on the remote when you last fetched.
  • git fetch updates those records. It does not touch your own branches or files.
  • git pull is git fetch, then a merge or rebase into the branch you are on.

Push the branch, then open a pull request

  • git push -u origin fix-login uploads the branch and links it to the remote one.
  • After that, plain git push and git pull know where to go.
  • A pull request proposes merging your branch into a base branch such as main.
  • Open it as a draft while work is unfinished. A draft cannot be merged.
git switch -c fix-login
git add -p
git commit -m "Fix login redirect loop"
git push -u origin fix-login
One piece of work: branch, stage, commit, push. Then open the pull request.
Git in four pages2 / 4