git switch
The command that moves you to another branch, updating HEAD and the working tree to match; -c creates the branch first.
In more detail
Added in Git 2.23 to take over branch switching from git checkout. git switch - goes back to the previous branch or commit, and git switch --detach moves to a commit without a branch. It refuses to switch if that would overwrite uncommitted changes.
Example
git switch -c experimentRelated terms
- branch — In Git, a movable name for one commit; committing on the branch moves the name forward to the new commit.
- HEAD — Git's pointer to where you are: normally the name of the current branch, and through it the current commit.
- git checkout — An older all-purpose command that switches branches and also restores files; newer Git splits these jobs into git switch and git restore.
- detached HEAD — The state where HEAD holds a commit ID directly instead of a branch name, so new commits are made but no branch moves to follow them.
Taught in
- Committing, And What A Good Message Says — Advanced Git Internals & Distributed Workflows
- A Branch Is A Moving Pointer — Advanced Git Internals & Distributed Workflows
- HEAD, And Why Detached HEAD Is Not An Error — Advanced Git Internals & Distributed Workflows
- Investigating History: blame, bisect And worktree — Advanced Git Internals & Distributed Workflows