Git Switch and Checkout
A musician who plays in two bands keeps two folders of sheet music. Before each rehearsal, they put away one folder and take out the other. The music on the stand changes completely, but nothing in either folder is lost.
git switch is that change of folder. It moves you to another branch, and Git updates the files in your project folder to match that branch's newest commit. Files that exist only on the other branch appear, files that exist only on this one disappear, and files that differ are rewritten. When you switch back, everything returns.
In this lesson, you will switch between branches, create a branch and switch to it in one step, and learn what Git does with changes you haven't committed yet. You will also meet git checkout, the older command that you will still see everywhere.
The Starting Point
Navika's project has two branches. On receipts, Advitha added a receipts file and changed the late fee:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
printf 'Term 1 fee: $12,000\nLate fee: $250\n' > fees.txt
git add .
git commit --quiet -m "Add README and fees"
git switch --quiet -c receipts
echo "Receipt No. 1" > receipts.txt
printf 'Term 1 fee: $12,000\nLate fee: $300\n' > fees.txt
git add .
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Add receipts and raise the late fee"
git switch --quiet main
git log --oneline --alld9d1344 (receipts) Add receipts and raise the late fee
bd27785 (HEAD -> main) Add README and feesSwitch to a Branch
git switch receiptsSwitched to branch 'receipts'Now look at the folder:
ls
cat fees.txt
git branchREADME.md
fees.txt
receipts.txt
Term 1 fee: $12,000
Late fee: $300
main
* receiptsreceipts.txt is here, and fees.txt has Advitha's late fee. The * shows the current branch. Switching back to main makes the folder match main again:
git switch --quiet main
ls
cat fees.txtREADME.md
fees.txt
Term 1 fee: $12,000
Late fee: $250receipts.txt has gone from the folder, but not from Git: it is safe in the receipts branch, and comes back when you switch there. --quiet only hides the "Switched to branch" message.
Create and Switch in One Step: -c
Most of the time you want a new branch and to start working on it. git switch -c (create) does both:
git switch -c feature/hostel-feeSwitched to a new branch 'feature/hostel-fee'The new branch starts at the commit you were on. To start it somewhere else, add the commit: git switch -c hotfix/fee-typo HEAD~1.
Jump Back: git switch -
A single - means "the branch I was on before", like the back button in a browser:
git switch -
git switch -Switched to branch 'main'
Switched to branch 'feature/hostel-fee'The first command went back to main, and the second returned to feature/hostel-fee.
Syntax
git switch <branch>
git switch -c <new-branch> [<start-commit>]| Command | It is used to |
|---|---|
git switch <branch> | Switch to an existing branch |
git switch -c <name> | Create a branch at the current commit and switch to it |
git switch -c <name> <commit> | Create a branch at another commit and switch to it |
git switch - | Switch to the previous branch |
git switch --discard-changes <branch> | Switch and throw away uncommitted changes |
git switch -m <branch> | Switch and try to carry uncommitted changes across |
git switch --detach <commit> | Look at a commit without being on a branch (next lesson) |
Switching with Uncommitted Changes
What happens to changes you haven't committed when you switch? Git's rule is simple: it never throws away your uncommitted work while switching (unless you ask it to with --discard-changes or --force). That leads to two cases.
Case 1: the changes come with you. Navika edits README.md, which is the same on both branches, and switches:
git switch --quiet main
echo "Tracks school fees." >> README.md
git switch receiptsM README.md
Switched to branch 'receipts'The switch worked, and M README.md tells you the edit came along. Uncommitted changes don't belong to any branch: they are in your working tree, and they stay there when you switch, as long as the file is the same in both branches.
Case 2: Git refuses. Now Navika goes back to main and edits fees.txt, which is different on receipts:
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git switch receiptserror: Your local changes to the following files would be overwritten by checkout:
fees.txt
Please commit your changes or stash them before you switch branches.
AbortingTo switch, Git would have to replace fees.txt with the version from receipts, which would overwrite the hostel line. So it stops, and nothing changes. You have four ways forward:
| Choice | Command |
|---|---|
| Commit the change on the current branch first | git commit -am "..." |
| Put the change aside, switch, and bring it back later | git stash, covered in a later lesson |
| Throw the change away and switch | git switch --discard-changes receipts |
| Try to carry the change over, merging it into the other version | git switch -m receipts |
Navika commits the hostel fee on main. She stages only fees.txt, because the README edit isn't finished yet:
git add fees.txt
git commit --quiet -m "Add the hostel fee"
git switch receiptsM README.md
Switched to branch 'receipts'The switch worked, and M README.md shows that the README edit came along again, because README.md is still the same on both branches.
Untracked Files
Untracked files belong to no branch either, so they stay in your folder whatever branch you are on. Git blocks a switch only when an untracked file has the same name as a tracked file on the other branch:
git switch --quiet main
echo "my own list" > receipts.txt
git switch receiptserror: The following untracked working tree files would be overwritten by checkout:
receipts.txt
Please move or remove them before you switch branches.
AbortingMoving or renaming your file, then switching, solves it. Here, Navika removes it:
rm receipts.txt
git switch --quiet receipts
git branch --show-currentreceiptsThe Older Command: git checkout
Before Git 2.23 (2019), git checkout did the work of both git switch and git restore. It still works, and older tutorials, scripts and answers use it everywhere:
| Job | git checkout form | Modern form |
|---|---|---|
| Switch to a branch | git checkout receipts | git switch receipts |
| Create and switch | git checkout -b feature/x | git switch -c feature/x |
| Switch to the previous branch | git checkout - | git switch - |
| Look at an old commit | git checkout a1b2c3d | git switch --detach a1b2c3d |
| Throw away a file's edits | git checkout -- fees.txt | git restore fees.txt |
The problem with git checkout is that one command does two very different things. git checkout receipts switches branches, but if receipts were also the name of a file, the same words could throw away that file's edits. git switch only ever switches branches, and git restore only ever restores files. Use the new commands in your own work, and read git checkout when you meet it.
git checkout main
git checkout -b feature/exam-feeM README.md
Switched to branch 'main'
Switched to a new branch 'feature/exam-fee'Common Mistakes
Switching with uncommitted work and forgetting it came along. The edit then gets committed on the wrong branch. Run git status after switching.
Using --discard-changes to get past the error. It throws away the uncommitted changes for good. Commit or stash instead, unless you really don't want them.
Typing git switch <commit-id>. git switch expects a branch and refuses a commit without --detach. The next lesson explains why.
Looking for files from another branch in your folder. Only the current branch's files are there. Switch, or read a file from another branch with git show receipts:receipts.txt.
Mixing up git checkout <branch> and git checkout -- <file>. The -- makes all the difference. With git switch and git restore, the mix-up can't happen.
Under the Hood
A switch does two things. It writes the new branch's name into .git/HEAD:
git switch --quiet receipts
cat .git/HEADref: refs/heads/receiptsThen it compares the snapshot of the commit you are leaving with the snapshot of the commit you are going to, and rewrites only the files that differ in the staging area and the working tree. Files that are the same are not touched at all. That is why switching is fast even in a large project, and why your uncommitted edits to such files survive: Git never needs to write those files. The refusal you saw happens when a file that must be rewritten also has uncommitted changes.
Interview Corner
What is the difference between git switch and git checkout?
git switch only changes branches. git checkout changes branches and also restores files, depending on its arguments, which made it easy to misuse. Git 2.23 split it into git switch (branches) and git restore (files). git checkout still works and is widely used.
How do you create a new branch and switch to it?
git switch -c <name>, or the older git checkout -b <name>. Add a commit at the end to start the branch somewhere other than the current commit.
What happens to uncommitted changes when you switch branches?
They stay in the working tree and come with you, because they don't belong to any branch. If switching would overwrite them (the file differs between the branches), Git refuses, and you commit, stash or discard them first, or use git switch -m to merge them across.
How do you go back to the branch you were on before?
git switch - (or git checkout -). It uses the reflog to find the previous branch.
Practice
Challenge: will it switch? In the same repository, you are on receipts. Edit README.md again by adding Run the app., then try git switch main. Then edit fees.txt by adding Exam fee: $800, and try git switch receipts.
What does Git print? Before you open the solution, decide which of the two switches works. Remember which files differ between the branches.
Show solution
echo "Run the app." >> README.md
git switch mainM README.md
Switched to branch 'main'The first switch works. README.md is the same on both branches, so the edit simply came along: M README.md. Now the second one:
echo 'Exam fee: $800' >> fees.txt
git switch receiptserror: Your local changes to the following files would be overwritten by checkout:
fees.txt
Please commit your changes or stash them before you switch branches.
AbortingThis one is refused. fees.txt differs between main and receipts, so switching would overwrite the new exam fee line.
Checkpoint. Work on two features in two branches. In one of them, leave an uncommitted change, and try to switch to the other: once with a file that is the same on both branches, and once with a file that differs. Then solve the second case by committing.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git switch <branch> | Switch to a branch |
git switch -c <name> | Create a branch and switch to it |
git switch - | Switch back to the previous branch |
git switch --discard-changes <branch> | Switch, throwing away uncommitted changes |
git switch -m <branch> | Switch, merging uncommitted changes across |
git branch --show-current | Print the current branch |
git checkout <branch> | Older form of git switch |
git checkout -b <name> | Older form of git switch -c |
Was this tutorial helpful?