Create and switch

Lesson 11 of 25 · Git: records, branches, and collaboration

Assume a repository with at least one commit on main and a clean working tree. git status shows the current branch and uncommitted changes. Create a branch and select it:

git status
git switch -c map-key
git branch

The asterisk in git branch marks the current branch. -c creates the new name at the current commit and switches to it. By contrast, git branch map-key creates a name without switching, and fails if that name already exists. To return later, use git switch main; git checkout main is an older alternative you may encounter.

Switching changes the tracked files in the working tree as needed for the selected commit. Uncommitted edits can sometimes travel with you; Git refuses a switch if it would overwrite conflicting changes. Inspect and safely commit or otherwise preserve work first. Switching branches does not itself create a commit.

After work is integrated and no longer needed, git branch -d map-key removes the local name, not the commits that remain reachable. Git checks whether its tip is merged into its upstream, or into HEAD if no upstream is configured. A refusal deserves inspection; forcing deletion can make unique commits hard to recover.

To rename the current local branch, git branch -m new-name changes its name, not its commits. Choose an unused name and coordinate separately if a remote or collaborators use the old name. The initial name of newly created branches can be configured with init.defaultBranch; git init -b main chooses a name explicitly for the example without changing a global default.