Practical guideTested1 min read
Create and switch to a new Git branch
Advertisement
What a branch is for
A branch is a separate line of work. You try an idea on it, and main stays untouched until you decide to merge.
git switch -c feature-x
git branchGo back with git switch main. Delete a merged branch with git branch -d feature-x.
Common mistakes
- Switching with uncommitted edits.
- Creating a name that exists.
- Typing a branch name that is not there.
- Deleting a branch with unmerged commits.
Does git switch -c copy my files?
No. It makes a new pointer at your current commit and moves you to it. Your files stay as they are.
What is the difference between git switch and git checkout?
git switch only changes branches, so it is harder to misuse. git checkout also restores files and does more than one job.
Source: https://git-scm.com/docs/git-switch and https://git-scm.com/docs/git-branch
Full kit: Git Survival Kit