ChatGPT解决这个技术问题 Extra ChatGPT

How can I switch to another branch in git?

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 checkout [branch] for most users coming to this question

E
ElpieKay

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>

This answer is correct (as usual, and upvoted), but I'll add a comment that may be helpful: the git checkout command does too many things, in my opinion. That's why there are so many modes of operation here. If the only thing git checkout did was switch branches, the answer would be simple, but it can also create branches, and even extract files from specific commits without switching branches.
this is the right answer, but shows how git is kinda screwed up in command line. git checkout to switch branch?
@thang Well, with release 2.23.0, this is remedied: you can now use git switch to switch to a branch.
Switch doesn’t seem to work for this version of git. What do I use to switch to a different branch in this version of git? C:\widget>git --version git version 2.11.0.windows.3 C:\widget>git switch master git: 'switch' is not a git command. See 'git --help'. C:\widget>
@John use git checkout instead for old versions, which also works in modern versions.
d
danglingpointer

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.


The last command gets me into detached HEAD state. Means one cannot edit the branch.
The branch your trying to checkout is not fetched then you need to fetch before checkout. You can skip the fetch if the branch is upto date then just use git checkout branchname.
Wouldn't it be sufficient to perform a "git pull" after having switched to the branch?
pull also ok, pull does the fetch and merge together in the background. I don't see any diff.
p
pola

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"


Thanks Bro, краткость сестра таланта next to reply above)
n
nnov

[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']


I know the command "git checkout -b branchName" for creating another branch. This was not the question!
g
gkw

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


Holy s**t, this is a game changer. Slightly related, you can use git restore for what checkout did to files.
K
Karam Qusai

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

T
Tshilidzi Mudau

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/*


I wonder how this series of commands would switch to another branch.
This might be useful to do when you did a shallow clone (using the depth param) previously and now wonder why you cannot fetch another remote branches getting the error: pathspec 'another_branch' did not match any file(s) known to git using the commands suggested above. It is surely not what the original question was about but it can help others scratching their heads here.
E
Elijah Ayandokun

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


R
Rohit Chaurasiya

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


V
Valdemar Vreeman

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.


M
Meet Bhalodiya

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

git switch branch-name


K
Kulamani

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

S
Singh

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


a
ahmed khan

The Best Command for changing branch

git branch -M YOUR_BRANCH

Please add what's the difference between the three commands below