如何切换到 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
How can I switch to another branch in git?
提问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_branch
already exists locally and you are not on this branch, then git checkout another_branch
switches to the branch.
如果another_branch
本地已经存在并且您不在此分支上,则git checkout 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
.
如果another_branch
不存在但origin/another_branch
存在,则git checkout another_branch
等价于git checkout -b another_branch origin/another_branch; git branch -u origin/another_branch
。这是创建another_branch
从origin/another_branch
和集origin/another_branch
为一体的上游another_branch
。
If neither exists, git checkout another_branch
returns error.
如果两者都不存在,则git checkout another_branch
返回错误。
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
大多数情况下返回错误。如果origin
是修订版并且another_branch
是文件,则它会检出该修订版的文件,但这很可能不是您所期望的。origin
在主要使用git fetch
,git pull
并git push
作为远程链接到远程存储库的一个别名。
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.
git checkout origin/another_branch
如果origin/another_branch
存在则成功。它导致处于分离的 HEAD 状态,而不是在任何分支上。如果您进行新提交,则无法从任何现有分支访问新提交,并且不会更新任何分支。
UPDATE:
更新:
As 2.23.0 has been released, with it we can also use git switch
to create and switch branches.
由于2.23.0已经发布,我们也可以使用它git switch
来创建和切换分支。
If foo
exists, try to switch to foo
:
如果foo
存在,请尝试切换到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
:
如果foo
不存在,origin/foo
存在,尝试创建foo
从origin/foo
,然后切换到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
:
更一般地,如果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, 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:
如果我们同时在 Gitlab 和 Github 维护一个仓库,那么本地仓库可能有两个远程,例如origin
Gitlab 和github
Github。在这种情况下,存储库具有origin/foo
和github/foo
。git switch foo
会抱怨fatal: invalid reference: foo
,因为它不知道从哪个 reforigin/foo
或github/foo
中创建foo
。我们需要根据需要git switch -c foo origin/foo
或git switch -c foo github/foo
根据需要指定它。如果我们想从两个远程分支创建分支,最好为新分支使用不同的名称:
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
:
如果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 branch
instead. 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
回答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