git switch
Switch to another branch, or create a new one and switch to it
What it does
git switch changes the current branch: the files in your folder become what they are in that branch's latest commit. With -c it creates the branch first and switches to it right away, the usual way to start a new task. git switch - takes you back to the previous branch.
Uncommitted edits come along with you, and if they get in the way (the same file differs in the other branch), git refuses to switch. Then commit the edits or put them aside with git stash. The command arrived in git 2.23; before that there were git checkout and git checkout -b, which still work.
Syntax
git switch <branch>
git switch -c <new-branch>
git switch -Examples
Switch to the main branch
git switch mainsame as:
git checkout mainswitch— switch branchesmain— where to
Create the branch feature/login and switch to it right away
git switch -c feature/loginsame as:
git checkout -b feature/login,git switch --create feature/loginswitch— switch branches-c— create a new onefeature/login— branch name
Related commands
- git branch — List, create, rename or delete branches
- git stash — Put uncommitted edits aside for a while and bring them back later
- git merge — Bring the changes from another branch into the current one