Create and switch to a new branch

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Creates a new branch from the current commit and switches to it, keeping any uncommitted changes in the working tree.

gitbranchswitch

Parameters

Name of the branch to create.

Commands

Confirm the branch the new branch will be created from

git branch --show-current

Create the new branch and switch to it

git switch -c <branch_name>

Verification

git branch --show-current

Prints the new branch name.

Undo

Return to the previous branch

git switch -

Delete the branch that was just created

git branch -d <branch_name>

Pitfalls

  • Fails if a branch with the same name already exists; use git switch to move to it instead.
  • Uncommitted changes travel with you to the new branch; stash first if that is not wanted.

Related