Git commands
How git switch -c branches off your current commit
git switch -c: a new branch from where you are
How to use this page
- slider / ▶Drag the slider, or press play, to watch the command happen. Before and After jump to either end.
- ← → · spaceStep through a multi-step command; space toggles Before / After.
- APlay or pause the loop (the page opens playing). Any manual input takes over.
- hoverA commit shows its message, author, date and parents, with its history highlighted.
- clickCopies the commit sha.
- ctrl + wheelZoom around the cursor (pinch on a trackpad). Double-click resets the view.
- EscStop playback, reset the view, close this menu.
- shareThe Share button copies a link to this graph, copies it as an image, downloads PNG/SVG/page, or posts it. #before, #after or #step=N in the link pins the state.
git-sim visually simulates any Git command in your own repos - from your terminal, IDE, or AI.
Table of Contents
- Introduction
- What is git switch -c?
- Watch it happen
- Before and after
- Starting from another commit
- Is it safe?
- How to undo it
- Try it on your repository
- Common questions
- Summary
- Next steps
- Related commands
Introduction
git switch -c is how a piece of work starts in modern Git: make a branch, be on it, one command. It's the same operation as git checkout -b, spelled with the branch-only command that Git 2.23 introduced.
In this article, we'll:
- Watch
git switch -c hotfixcreate a branch and moveHEADonto it - Check what changed and what didn't
- Look at branching from an older commit
What is git switch -c?
git switch -c <name> creates a new branch pointing at the current commit and switches to it. Under the hood that's a new file in .git/refs/heads/ and a one-line change to .git/HEAD. Because the new branch starts at the commit you're already on, no files change.
Watch it happen
Our sample repo has a new branch created from the current commit. Here's git switch -c hotfix:
- Before:
HEADis attached tomainat8c02d5b. There is nohotfixbranch yet. - Git creates the
hotfixbranch at8c02d5b, alongsidemain, and attachesHEADto it. maindoesn't move, and no commits are created.
Before and after
After the command, two branches share the commit 8c02d5b, HEAD is on the new one, and git status says hotfix. From here, the next commit moves hotfix forward and leaves main behind, which is the whole point.
The raw git output, if you want to read along in text
git log --oneline --graph --all
before
* 1117a34 (feature) Add search tests
* e5869f0 Fix typo in search box
* fc19889 Add search box
| * 8c02d5b (HEAD -> main) Update dependencies
| * a0b2db3 Add user settings page
|/
* 96c4fc2 Fix header layout
* ae65976 Add login page
* 3e1ffe4 Add project skeleton
* 4114b2c Initial commitafter
* 1117a34 (feature) Add search tests
* e5869f0 Fix typo in search box
* fc19889 Add search box
| * 8c02d5b (HEAD -> hotfix, main) Update dependencies
| * a0b2db3 Add user settings page
|/
* 96c4fc2 Fix header layout
* ae65976 Add login page
* 3e1ffe4 Add project skeleton
* 4114b2c Initial commitwhat git printed
Switched to a new branch 'hotfix'Starting from another commit
git switch -c hotfix 96c4fc2
creates the branch at 96c4fc2 instead, and switches to it. Your working directory changes to match that commit, so this form does touch files.
main by accident are easy enough to move, but it's one more thing to fix, and git switch -c is short enough that there's no reason to wait.
Is it safe?
Safe git-sim pre-flight
Switches to 'hotfix'.
Creating and switching at the current commit changes one ref file and HEAD. Uncommitted changes carry over untouched.
How to undo it
git switch main
git branch -d hotfix
The branch points at the same commit as main, so Git deletes it as merged.
Try it on your repository
pip install git-sim
git-sim switch -c hotfix
git-sim shows the new label and the HEAD move on your own graph.
Common questions
What does git switch -c do?
Creates a branch at the current commit and switches to it. -c stands for create. The git switch documentation also lists -C, which resets a branch that already exists instead of refusing.
What is the difference between git switch -c and git checkout -b?
Nothing in effect. switch -c is the newer spelling, introduced when Git split checkout into switch and restore.
How do I create a branch from a specific commit with git switch?
git switch -c <name> <commit>.
Does git switch -c change my files?
Not when branching from the current commit. Branching from another commit updates the working directory to match it.
Summary
In this article, we watched git switch -c create a hotfix branch on the current commit and move HEAD onto it, confirmed nothing else changed, and saw how to start the branch from an older commit.
Next steps
git commit is the next thing you'll do on the new branch, and git merge is how the work gets back.
Related commands
- git checkout -b, the older spelling
- git branch, to create without switching
- git switch, to switch without creating
- git checkout, the original all-purpose command
- git commit, to put work on the branch
- git merge, to bring it back
