Making and switching
Last updated
To work on a branch you switch to it: Git changes the files in your folder to match. In this lesson you'll make a branch and switch in one command, watch files appear and disappear as you switch, see what happens to unfinished changes, and delete branches you're done with.
By the end of this lesson you can
- Make and switch to a branch with git switch -c, and switch back with git switch
- Say what happens to your files, and to uncommitted changes, when you switch
- Delete a branch with git branch -d, and know when -D is needed
- Read older instructions that use git checkout
Switching changes your files
git switch <branch> moves HEAD to that branch, and changes the files in your working folder to match its commit. Files the branch has appear; files it doesn't have disappear; changed files change.
Nothing is lost: everything is in the commits, and switching back brings it all back.
$ git branch -v desserts 647fe2c Move leche flan into desserts * main 67e435d Cover the leche flan with foil $ ls README.md adobo.md build_index.py leche-flan.md lumpia.md pancit.md sinigang.md
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe
and 2 older commits
git switch - switches to the branch you were on before, like the back button.
Work on a branch
git switch -c <name> makes a branch and switches to it in one go (-c for create). Then commit as usual: the commits go on the new branch, and main stays where it was.
- Make a branch called
dessertsand switch to it, in one command - Make a file
turon.mdand commit it on desserts - Switch back to main and check that turon.md isn’t there
- Switch to desserts again and see it come back
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
You are in ~/kusina, which has ten commits from two weeks of work.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Unfinished changes and switching
What about changes you haven't committed? They belong to no branch: they're in your working folder. So:
- If the other branch has the same version of the files you changed, Git carries your changes across to the other branch.
- If switching would overwrite your changes, Git refuses, and says so:
git switch desserts error: Your local changes to the following files would be overwritten by checkout: leche-flan.md Please commit your changes or stash them before you switch branches. Aborting
Git never throws away your uncommitted work when switching. Do what the message says: commit, or git stash, then switch. git switchCareful: changes files or history, but Git can usually get it back: your files change, but nothing is lost.
Deleting branches
Once a branch's work has been merged (lesson 3.3), the label isn't needed. git branch -d desserts deletes it. The commits stay, because main now includes them.
If the branch has commits that aren't merged anywhere, -d refuses, so you don't lose work by accident:
git branch -d more-vinegar error: the branch 'more-vinegar' is not fully merged hint: If you are sure you want to delete it, run 'git branch -D more-vinegar' hint: Disable this message with "git config set advice.forceDeleteBranch false"
git branch -DCan lose work: can throw away work Git never saved, or rewrite shared history deletes it anyway. Its commits become dashed, and only the reflog can find them, for about a month.
- Try to delete
dessertswith-dand read why Git refuses - Make a branch
tryout, then delete it with-d(it has nothing unmerged, so Git allows it)
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
You are in ~/kusina on main. A branch called desserts has two commits that main doesn’t.
Commit graph
647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
You can't delete the branch you're on. Switch away first.
git checkout, in older instructions
Before Git 2.23 (2019), one command did both switching branches and restoring files: git checkout. It still works, and you'll see it in older tutorials and answers:
| Older instructions say | This course says |
|---|---|
git checkout desserts | git switch desserts |
git checkout -b desserts | git switch -c desserts |
git checkout -- adobo.md | git restore adobo.md |
git checkout abc1234 | git switch --detach abc1234 (lesson 3.5) |
switch and restore were added to split those jobs, so one command can't do the other's damage by mistake.
Spot the trap: an hour on the wrong branch
Ana made a desserts branch yesterday. Today she types up three dessert recipes and commits them. Then she runs git branch and sees * main.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-switch: -c, switching with local changes, and -
- Git documentation: git-branch, -d and -D: deleting merged and unmerged branches
- Git documentation: git-checkout: the older command for switching branches and restoring files
- Pro Git: Basic Branching and Merging: switching with a clean working folder