如何在git中切换到另一个分支? [英] How can I switch to another branch in git?

查看:1900
本文介绍了如何在git中切换到另一个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git checkout 'another_branch'

git checkout origin 'another_branch'

git checkout origin/'another_branch'


它们之间有什么区别?



And what is the difference between them?


推荐答案

如果another_branch已在本地存在,而您不在此分支上,则git checkout another_branch切换到该分支.

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

如果another_branch不存在,而origin/another_branch存在,则git checkout another_branch等效于git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch.这是从origin/another_branch创建another_branch并将origin/another_branch设置为another_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.

如果都不存在,则git checkout another_branch返回错误.

If neither exists, git checkout another_branch returns error.

git checkout origin another_branch在大多数情况下会返回错误.如果origin是修订版本,而another_branch是文件,则它将检出该修订版本的文件,但很可能不是您所期望的. origin通常在git fetchgit pullgit push中用作远程文件,是远程存储库URL的别名.

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成功.它导致处于分离的HEAD状态,而不是在任何分支上.如果您进行新的提交,则任何现有分支都无法访问新的提交,并且不会更新任何分支.

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.

更新:

随着2.23.0版本的发布,我们也可以使用git switch创建和切换分支.

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

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

If foo exists, try to switch to foo:

git switch foo

如果foo不存在并且origin/foo存在,请尝试从origin/foo创建foo,然后切换到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

通常,如果foo不存在,请尝试从已知的引用或提交创建foo,然后切换到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>

如果我们同时在Gitlab和Github中维护一个存储库,则本地存储库可能具有两个远程服务器,例如,origin用于Gitlab,而github用于Github.在这种情况下,存储库具有origin/foogithub/foo. git switch foo会投诉fatal: invalid reference: foo,因为它不知道从哪个引用origin/foogithub/foo来创建foo.我们需要根据需要用git switch -c foo origin/foogit switch -c foo github/foo指定它.如果要从两个远程分支创建分支,最好为新分支使用可区分的名称:

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

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

等同于:

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

尝试切换到已知引用或提交的独立HEAD:

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

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

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

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>

这篇关于如何在git中切换到另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆