Switching and Managing Branches

A branch is a movable reference to a commit that isolates different lines of work. This course uses the single-purpose git switch for branch switching while retaining compatible git checkout forms. Automation should still verify Git version and project conventions in the target environment. See Git State Model and Commit Graph for the state model.

git switch <name>

git switch feature/demo

Purpose: switch to an existing branch.

Legacy form:

git checkout feature/demo

Appropriate when:

  • switching from the current branch to main, develop, or a feature branch;
  • inspecting code state on another branch.

If the working tree has uncommitted changes, Git may refuse to switch to avoid overwriting your work. Run git status --short first.

git switch -c <name>

git switch -c feature/demo

Purpose: create a branch from the current commit and immediately switch to it.

Legacy form:

git checkout -b feature/demo

Appropriate when:

  • starting a feature, fix, or documentation-cleanup task;
  • isolating experimental work from the main branch.

Example:

git switch -c docs/git-cheat-sheet

Before creation, use git branch --show-current to confirm the actual baseline branch name. git init default depends on version, installer, or config; a clone’s initial branch comes from remote HEAD, so it is not always main.

git branch

git branch

Purpose: list local branches; the current branch normally has a marker.

Common extensions:

git branch -a
git branch -vv
  • -a also shows remote-tracking branches.
  • -vv shows upstream and ahead/behind information for local branches.

git branch --sort=-committerdate

git branch --sort=-committerdate

Purpose: list branches descending by latest commit time.

Appropriate when:

  • finding recently active feature branches;
  • determining which branches have been inactive before cleanup.

git branch -d <name>

git branch -d feature/demo

Purpose: delete a local branch. Git checks whether the branch was merged into its upstream; if it has no upstream, it checks whether it was merged into current HEAD. It refuses when those protection conditions are not met.

Appropriate when:

  • a feature branch is merged;
  • a local temporary branch is no longer needed.

You cannot delete the current branch; switch elsewhere first.

git branch -D <name>

git branch -D feature/demo

Purpose: force-delete a local branch even when Git considers it unmerged.

High-risk operation

-D removes the branch reference to unmerged commits. Before running it, use git log --oneline HEAD..feature/demo to view commits unique to the target branch, then git branch --contains feature/demo to see which branches contain its tip. If still uncertain, do not force-delete.

Recovery idea:

  • If it was deleted recently by mistake, use git reflog to locate a commit ID and create the branch again.

Next: merge branches

Creating and switching solves isolation, not reconciliation. Continue with Branch Merging and Conflict Resolution for fast-forward, three-way merge, conflict markers, git merge --continue, and git merge --abort.