branching
git branch
Lists, creates, renames, or deletes branches — pointers to commits. Does not switch branches on its own; use switch or checkout for that.
git branch [<options>] [<name> [<start-point>]] Common flags
| Flag | Purpose |
|---|---|
| -a | List local and remote-tracking branches |
| -r | List only remote-tracking branches |
| -d <name> | Delete a fully merged branch |
| -D <name> | Force delete a branch even if unmerged |
| -m <old> <new> | Rename a branch |
| --set-upstream-to=<upstream> | Configure tracking for the current branch |
| -vv | Verbose list showing upstream tracking info |
Examples
git branch List local branches; asterisk marks the current one
git branch -d feature/login Delete a merged feature branch
git branch -m main master-old Rename a branch locally
Gotcha
Deleting the local branch does not delete it on the remote; use 'git push origin --delete
Related commands
git switch
Modern, focused command (Git 2.23+) that only switches branches — no file restoration overloading like checkout. Safer and clearer for everyday branch changes.
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.
git merge
Joins the history of another branch into the current one, creating a merge commit when histories diverge. Preserves the full branching topology.
git rebase
Replays commits from your branch on top of another base, producing a linear history without merge commits. Powerful for cleaning up history before pushing.