如何切换到 git 中的另一个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47630950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:12:21  来源:igfitidea点击:

How can I switch to another branch in git?

gitgithubversion-controlgitlabgit-checkout

提问by Benyamin Jafari

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 these lines?

这些线之间有什么区别?



回答by ElpieKay

If another_branchalready exists locally and you are not on this branch, then git checkout another_branchswitches to the branch.

如果another_branch本地已经存在并且您不在此分支上,则git checkout another_branch切换到该分支。

If another_branchdoes not exist but origin/another_branchdoes, then git checkout another_branchis equivalent to git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch. That's to create another_branchfrom origin/another_branchand set origin/another_branchas the upstream of another_branch.

如果another_branch不存在但origin/another_branch存在,则git checkout another_branch等价于git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch。这是创建another_branchorigin/another_branch和集origin/another_branch为一体的上游another_branch

If neither exists, git checkout another_branchreturns error.

如果两者都不存在,则git checkout another_branch返回错误。

git checkout origin another_branchreturns error in most cases. If originis a revision and another_branchis a file, then it checks out the file of that revision but most probably that's not what you expect. originis mostly used in git fetch, git pulland git pushas a remote, an alias of the url to the remote repository.

git checkout origin another_branch大多数情况下返回错误。如果origin是修订版并且another_branch是文件,则它会检出该修订版的文件,但这很可能不是您所期望的。origin在主要使用git fetchgit pullgit push作为远程链接到远程存储库的一个别名。

git checkout origin/another_branchsucceeds if origin/another_branchexists. 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.

git checkout origin/another_branch如果origin/another_branch存在则成功。它导致处于分离的 HEAD 状态,而不是在任何分支上。如果您进行新提交,则无法从任何现有分支访问新提交,并且不会更新任何分支。

UPDATE:

更新

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

由于2.23.0已经发布,我们也可以使用它git switch来创建和切换分支。

If fooexists, try to switch to foo:

如果foo存在,请尝试切换到foo

git switch foo

If foodoes not exist and origin/fooexists, try to create foofrom origin/fooand then switch to foo:

如果foo不存在,origin/foo存在,尝试创建fooorigin/foo,然后切换到foo

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

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

更一般地,如果foo不存在,请尝试foo从已知的引用或提交创建,然后切换到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, originfor Gitlab and githubfor Github. In this case the repository has origin/fooand github/foo. git switch foowill complain fatal: invalid reference: foo, because it does not known from which ref, origin/fooor github/foo, to create foo. We need to specify it with git switch -c foo origin/fooor git switch -c foo github/fooaccording to the need. If we want to create branches from both remote branches, it's better to use distinguishing names for the new branches:

如果我们同时在 Gitlab 和 Github 维护一个仓库,那么本地仓库可能有两个远程,例如originGitlab 和githubGithub。在这种情况下,存储库具有origin/foogithub/foogit switch foo会抱怨fatal: invalid reference: foo,因为它不知道从哪个 reforigin/foogithub/foo中创建foo。我们需要根据需要git switch -c foo origin/foogit switch -c foo github/foo根据需要指定它。如果我们想从两个远程分支创建分支,最好为新分支使用不同的名称:

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

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

如果foo存在,请尝试foo从(或重置foo为)已知引用或提交重新创建/强制创建,然后切换到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:

尝试切换到已知引用或提交的分离 HEAD:

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

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

如果您只想创建一个分支而不切换到它,请git branch改用。尝试从已知的引用或提交创建一个分支:

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

回答by danglingpointer

Switching to another branch in git. Straightforward answer,

切换到 git 中的另一个分支。直截了当的回答,

git-checkout - Switch branches or restore working tree files

git-checkout - 切换分支或恢复工作树文件

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.

在切换分支之前,请确保您没有任何修改过的文件,在这种情况下,您可以提交更改或隐藏它。

回答by Mehdi

[git checkout "branch_name"]

[ git checkout "branch_name"]

is another way to say:

是另一种说法:

[git checkout -b branch_name origin/branch_name]

[ git checkout -b branch_name origin/branch_name]

in case "branch_name" exists onlyremotely.

如果“branch_name”远程存在。

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

[ git checkout -b branch_name origin/branch_name] 在您有多个遥控器的情况下很有用。

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

关于 [ git checkout origin 'another_branch'] 我不确定这是可能的,AFAK 你可以使用“fetch”命令来做到这一点——[ git fetch origin 'another_branch']

回答by gkw

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

Git 2.23开始,可以使用git switch <branch name>切换分支。

回答by 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

回答by Matthew Joughin

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

如果您希望分支跟踪远程分支,如果您要向分支提交更改并拉取更改等,这非常重要,您需要使用添加 -t 进行实际结帐,例如: git checkout -t branchname

回答by 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

回答by pavan

Check : git branch -a

查看 : 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/*
  • 第1步 : git config --list
  • 第2步 : git config --unset remote.origin.fetch
  • 第 3 步: git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

回答by 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]

git switch [branchName] 或 git checkout [branchName]

ex: git switch develop OR
git checkout develop

例如: git switch develop 或
git checkout develop