Skip to content
Log In Sign Up info@muvcloud.com

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:

checkoutNewer command
git checkout maingit switch main
git checkout -b devgit switch -c dev
git checkout -git switch -
git checkout v1.2.0git switch --detach v1.2.0
git checkout -- app.jsgit 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:

Common Errors

  • Pathspec did not match: the branch does not exist locally and Git does not know it from a remote. Run git fetch and check the name with git 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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations