Git Branches


What is a Branch?

  • Allows to keep track of different changes separately.
  • Used to develop features, fixes, or experiments safely without affecting main code.

alt text


HEAD and Branch

  • HEAD points to the current branch

  • Switching branches moves HEAD

  • Check active branch: cat .git/HEAD or git branch

alt text

alt text


Branch Commands

List branches

  • git branch → local branches
  • git branch -a → all branches (local + remote)

Create a branch

  • git branch bug-fix

Switch branch

  • git checkout bug-fix
  • git switch bug-fix

Create & switch

  • git checkout -c bug-fix
  • git switch -c bug-fix

Rename branch

  • git branch -m old-name new-name
  • git branch -M main

Delete branch

  • git branch -d bug-fix
  • git branch -D bug-fix (force)

alt text

Remote Branches

  • git branch -r→View remote branches
  • git push origin bug-fix→Push a branch
  • git push origin --delete bug-fix→Delete remote branch

Best Practices

  • Commit before switching branches
  • One feature or fix per branch
  • Delete branches after merge