How can I switch to another branch in git?

GitVersion ControlGit Checkout

Git Problem Overview


Which one of these lines is correct?

git checkout 'another_branch'

or

git checkout origin 'another_branch'

or

git checkout origin/'another_branch'

And what is the difference between them?

Git Solutions


Solution 1 - Git

If another_branch already exists locally and you are not on this branch, then git checkout another_branch switches to the branch.

If another_branch does not exist but origin/another_branch does, then git checkout another_branch is equivalent to git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch. That's to create another_branch from origin/another_branch and set origin/another_branch as the upstream of another_branch.

If neither exists, git checkout another_branch returns error.

git checkout origin another_branch returns error in most cases. If origin is a revision and another_branch is a file, then it checks out the file of that revision but most probably that's not what you expect. origin is mostly used in git fetch, git pull and git push as a remote, an alias of the url to the remote repository.

git checkout origin/another_branch succeeds if origin/another_branch exists. It leads to be in detached HEAD state, not on any branch. If you make new commits, the new commits are not reachable from any existing branches and none of the branches will be updated.

UPDATE:

As 2.23.0 has been released, with it we can also use git switch to create and switch branches.

If foo exists, try to switch to foo:

git switch foo

If foo does not exist and origin/foo exists, try to create foo from origin/foo and then switch to foo:

git switch -c foo origin/foo
# or simply
git switch foo

More generally, if foo does not exist, try to create foo from a known ref or commit and then switch to foo:

git switch -c foo <ref>
git switch -c foo <commit>

If we maintain a repository in Gitlab and Github at the same time, the local repository may have two remotes, for example, origin for Gitlab and github for Github. In this case the repository has origin/foo and github/foo. git switch foo will complain fatal: invalid reference: foo, because it does not known from which ref, origin/foo or github/foo, to create foo. We need to specify it with git switch -c foo origin/foo or git switch -c foo github/foo according to the need. If we want to create branches from both remote branches, it's better to use distinguishing names for the new branches:

git switch -c gitlab_foo origin/foo
git switch -c github_foo github/foo

If foo exists, try to recreate/force-create foo from (or reset foo to) a known ref or commit and then switch to foo:

git switch -C foo <ref>
git switch -C foo <commit>

which are equivalent to:

git switch foo
git reset [<ref>|<commit>] --hard

Try to switch to a detached HEAD of a known ref or commit:

git switch -d <ref>
git switch -d <commit>

If you just want to create a branch but not switch to it, use git branch instead. Try to create a branch from a known ref or commit:

git branch foo <ref>
git branch foo <commit>

Solution 2 - Git

Switching to another branch in git. Straightforward answer,

git-checkout - Switch branches or restore working tree files

git fetch origin         <----this will fetch the branch
git checkout branch_name <--- Switching the branch

Before switching the branch make sure you don't have any modified files, in that case, you can commit the changes or you can stash it.

Solution 3 - Git

Useful commands to work in daily life:

git checkout -b "branchname" ->  creates new branch
git branch                   ->  lists all branches
git checkout "branchname"    ->  switches to your branch
git push origin "branchname" ->  Pushes to your branch
git add */filename           -> Stages *(All files) or by given file name
git commit -m "commit message" -> Commits staged files
git push                     -> Pushes to your current branch

If you want to merge to dev from feature branch, First check out dev branch with command "git branch dev/develop" Then enter merge commadn "git merge featurebranchname"

Solution 4 - Git

[git checkout "branch_name"]

is another way to say:

[git checkout -b branch_name origin/branch_name]

in case "branch_name" exists only remotely.

[git checkout -b branch_name origin/branch_name] is useful in case you have multiple remotes.

Regarding [git checkout origin 'another_branch'] I'm not sure this is possible, AFAK you can do this using "fetch" command -- [git fetch origin 'another_branch']

Solution 5 - Git

With Git 2.23 onwards, one can use git switch <branch name> to switch branches.

Solution 6 - Git

What worked for me is the following:

> Switch to the needed branch:

git checkout -b BranchName

> And then I pulled the "master" by:

git pull origin master

Solution 7 - Git

Check : git branch -a

If you are getting only one branch. Then do below steps.

  • Step 1 : git config --list
  • Step 2 : git config --unset remote.origin.fetch
  • Step 3 : git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

Solution 8 - Git

If you want the branch to track the remote branch, which is very important if you're going to commit changes to the branch and pull changes etc, you need to add a -t for the actual checkout like so: git checkout -t branchname

Solution 9 - Git

I am using this to switch one branch to another anyone you can use it works for me like charm.

git switch [branchName] OR git checkout [branchName]

ex: git switch develop OR
git checkout develop

Solution 10 - Git

To switch to an branch with your changes you should do a fetch first. This is to save the changes like your package.json or your .env files

So:
git fetch

And then:
git checkout <new branch>

This answer is for those which where stuck for a while, like me.

Solution 11 - Git

Switching to another branch in git can be done with a single command.

git switch branch-name

Solution 12 - Git

Check remote branch list:

git branch -a

Switch to another Branch:

git checkout -b <local branch name> <Remote branch name>
Example: git checkout -b Dev_8.4 remotes/gerrit/Dev_8.4

Check local Branch list:

git branch

Update everything:

git pull

Solution 13 - Git

These are the steps I follow:

  • git clone {link}
  • cd {repo folder}

You can check the status and which branch you are on using:

  • git status
  • git branch
  • git branch -a

Note: Here if you make changes in your local repo before moving to the new branch, the following steps should still work.

If "git branch" shows master, and you want to create+move to another branch:

  • git checkout -b {branch name}

Check branch again using "git branch" It should now show that you are in the new branch.

Now add, commit and push:

  • git add .
  • git commit -m "added new branch"
  • git push origin {branch name}

The above steps work for me in both the situation when I have made changes before moving to the new local branch or making changes after moving to the new branch. I hope it helps people running into similar situations and it is also the solution to the question mentioned here: Link

Solution 14 - Git

The Best Command for changing branch

git branch -M YOUR_BRANCH

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenyamin JafariView Question on Stackoverflow
Solution 1 - GitElpieKayView Answer on Stackoverflow
Solution 2 - GitdanglingpointerView Answer on Stackoverflow
Solution 3 - GitpolaView Answer on Stackoverflow
Solution 4 - GitMehdiView Answer on Stackoverflow
Solution 5 - GitgkwView Answer on Stackoverflow
Solution 6 - GitKaram QusaiView Answer on Stackoverflow
Solution 7 - GitpavanView Answer on Stackoverflow
Solution 8 - GitMatthew JoughinView Answer on Stackoverflow
Solution 9 - GitRohit ChaurasiyaView Answer on Stackoverflow
Solution 10 - GitValdemar VreemanView Answer on Stackoverflow
Solution 11 - GitMeet BhalodiyaView Answer on Stackoverflow
Solution 12 - GitKulamaniView Answer on Stackoverflow
Solution 13 - GitSinghView Answer on Stackoverflow
Solution 14 - Gitahmed khanView Answer on Stackoverflow