Switch to a Branch
git checkout main
Git updates the files in your working tree to match the last commit of main and makes it the current branch. New commits now go onto main. To go back to the branch you were on before, use a dash:
git checkout -
Create a Branch and Switch to It
git checkout -b feature/login
The new branch starts at the commit you are on. To start it somewhere else, name the start point at the end, for example another branch or a tag:
git checkout -b hotfix/1.2.1 v1.2.0
More ways to start a branch are in how to create a branch in Git.
Check Out a Remote Branch
Fetch first, so your clone knows the branches on the remote, then check out the branch by its short name:
git fetch origin
git checkout feature/api
When exactly one remote has a branch with that name, Git creates a local branch of the same name that tracks it. To be explicit, or when several remotes have the name:
git checkout -b feature/api --track origin/feature/api
Checking out origin/feature/api itself does not create a local branch; it leaves you in a detached HEAD.
Check Out a Tag or a Commit
git checkout v1.2.0
git checkout a1b2c3d
Both put the files at that point in history without a branch: HEAD points straight at a commit. This is useful for testing an old version. Commits you make there belong to no branch, so read Git detached HEAD before you start working in that state.
Restore a File
Warning: the next command overwrites your uncommitted edits in that file, and they cannot be recovered.
git checkout -- config.php
The file goes back to its staged version, or to the last commit if nothing is staged. The -- tells Git that what follows is a path, not a branch name. To take a file from an older commit instead, name the commit first; the old version is written to your working tree and staged:
git checkout a1b2c3d -- config.php
git switch and git restore
Git 2.23 split checkout's two jobs into two commands with clearer names. checkout still works and is not going away, but the new commands are harder to misuse:
| checkout | Newer command |
|---|---|
git checkout main | git switch main |
git checkout -b dev | git switch -c dev |
git checkout - | git switch - |
git checkout v1.2.0 | git switch --detach v1.2.0 |
git checkout -- app.js | git restore app.js |
git switch refuses to detach HEAD unless you ask with --detach, which prevents the surprise of working without a branch. To restore a file from an older commit with git restore, add --source a1b2c3d.
When Uncommitted Changes Block a Switch
If a file you changed differs between the two branches, Git stops and says your local changes would be overwritten. Changes in other files are carried over to the new branch. You have three ways forward:
- Commit: finish the work on the current branch first.
- Stash: run
git stash, switch, and later bring the changes back withgit stash pop. See how to use git stash. - Discard: only if you are sure you do not need the edits, as described in how to discard local changes in Git.
Common Errors
- Pathspec did not match: the branch does not exist locally and Git does not know it from a remote. Run
git fetchand check the name withgit branch -a. - Ambiguous name: a branch and a file share the name. Add
--after a branch name or before a path to make the meaning clear.