Branches · lesson 2 of 7
Commands and ideas in this lesson
Agit switch -cmake a branch and switch to it
Bgit switchswitch to a branch
Cgit branch -ddelete a merged branch
Dgit checkoutthe older command that also switches

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

  1. 647fe2c desserts Move leche flan into desserts
  2. cfe7887 Add turon recipe
  3. 67e435d HEAD →main Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe

and 2 older commits

1 of 3 On main. The desserts branch has two commits main doesn’t: turon was added, and leche flan moved into a desserts folder.

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.

Git sandbox
  1. Make a branch called desserts and switch to it, in one command
  2. Make a file turon.md and commit it on desserts
  3. Switch back to main and check that turon.md isn’t there
  4. 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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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:
Terminal
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:

Terminal
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.

Git sandbox
  1. Try to delete desserts with -d and read why Git refuses
  2. 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

  1. 647fe2c desserts Move leche flan into desserts
  2. cfe7887 Add turon recipe
  3. 67e435d HEAD →main Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe
  11. 1f359a2 Add sinigang recipe
  12. 601c654 Add 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 sayThis course says
git checkout dessertsgit switch desserts
git checkout -b dessertsgit switch -c desserts
git checkout -- adobo.mdgit restore adobo.md
git checkout abc1234git 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

Spot the trapAna

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

Which command makes a branch called fix-flan and switches to it?
You have an uncommitted change to adobo.md, and the other branch has a different adobo.md. What happens when you switch?
git branch -d tryout says the branch is not fully merged. What does that mean?

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.

All sources for Simple Git