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/demoPurpose: switch to an existing branch.
Legacy form:
git checkout feature/demoAppropriate 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/demoPurpose: create a branch from the current commit and immediately switch to it.
Legacy form:
git checkout -b feature/demoAppropriate when:
- starting a feature, fix, or documentation-cleanup task;
- isolating experimental work from the main branch.
Example:
git switch -c docs/git-cheat-sheetBefore 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 branchPurpose: list local branches; the current branch normally has a marker.
Common extensions:
git branch -a
git branch -vv-aalso shows remote-tracking branches.-vvshows upstream and ahead/behind information for local branches.
git branch --sort=-committerdate
git branch --sort=-committerdatePurpose: 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/demoPurpose: 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/demoPurpose: force-delete a local branch even when Git considers it unmerged.
High-risk operation
-Dremoves the branch reference to unmerged commits. Before running it, usegit log --oneline HEAD..feature/demoto view commits unique to the target branch, thengit branch --contains feature/demoto see which branches contain its tip. If still uncertain, do not force-delete.
Recovery idea:
- If it was deleted recently by mistake, use
git reflogto 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.