如何切换到git中的另一个分支? 以下哪一行是正确的?

如何切换到git中的另一个分支? 以下哪一行是正确的?,git,github,version-control,gitlab,git-checkout,Git,Github,Version Control,Gitlab,Git Checkout,或 git checkout origin 'another_branch' git checkout origin/'another_branch' 或 git checkout origin 'another_branch' git checkout origin/'another_branch' 它们之间有什么区别? 如果另一个分支已在本地存在,而您不在该分支上,则git checkout另一个分支将切换到该分支 如果other_branch不存在,但origin/other_b

git checkout origin 'another_branch'
git checkout origin/'another_branch'

git checkout origin 'another_branch'
git checkout origin/'another_branch'

它们之间有什么区别?

如果
另一个分支
已在本地存在,而您不在该分支上,则
git checkout另一个分支
将切换到该分支


如果
other_branch
不存在,但
origin/other_branch
存在,则
git checkout other_branch
相当于
git checkout-b other_branch origin/other_branch;git分支机构-u源/另一个分支机构
。这就是从
origin/other\u branch
创建
other\u branch
,并将
origin/other\u branch
设置为
other\u branch
的上游

如果两者都不存在,
git checkout另一个分支
返回错误

git checkout origin另一个分支在大多数情况下返回错误。如果
origin
是一个修订版,而
另一个分支
是一个文件,那么它将检查该修订版的文件,但很可能这不是您所期望的
origin
主要用于
git fetch
git pull
git push
作为远程存储库url的别名

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

更新

随着2.23.0的发布,我们也可以使用它来创建和切换分支

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

git switch foo
git switch -c foo origin/foo
# or simply
git switch foo
git switch -c foo <ref>
git switch -c foo <commit>
git switch -C foo <ref>
git switch -C foo <commit>
如果
foo
不存在且
origin/foo
存在,请尝试从
origin/foo
创建
foo
,然后切换到
foo

git switch foo
git switch -c foo origin/foo
# or simply
git switch foo
git switch -c foo <ref>
git switch -c foo <commit>
git switch -C foo <ref>
git switch -C foo <commit>
更一般地说,如果
foo
不存在,请尝试从已知的ref或commit创建
foo
,然后切换到
foo

git switch foo
git switch -c foo origin/foo
# or simply
git switch foo
git switch -c foo <ref>
git switch -c foo <commit>
git switch -C foo <ref>
git switch -C foo <commit>
如果存在
foo
,尝试从(或将
foo
重置为)已知的ref或commit重新创建/强制创建
foo
,然后切换到
foo

git switch foo
git switch -c foo origin/foo
# or simply
git switch foo
git switch -c foo <ref>
git switch -c foo <commit>
git switch -C foo <ref>
git switch -C foo <commit>
[
git签出“分支机构名称”
]

另一种说法是:

[
git签出-b分支机构名称来源/分支机构名称
]

如果“分支机构名称”仅远程存在

[
git checkout-b branch\u name origin/branch\u name
]在您有多个遥控器的情况下非常有用

关于[
git checkout origin'other_branch'
],我不确定这是否可行,AFAK您可以使用“fetch”命令执行此操作
--[
git获取来源“另一个分支”
]

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

git签出-切换分支或还原工作树文件

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

git获取来源检查:
git分支-a

如果你只得到一个分支。然后执行以下步骤

  • 第1步:
    git配置--list
  • 第2步:
    git-config——取消设置remote.origin.fetch
  • 步骤3:
    git-config——添加remote.origin.fetch+refs/heads/*:refs/remotes/origin/*

如果您希望分支跟踪远程分支,这对于您将更改提交到分支并获取更改等非常重要,则需要为实际签出添加一个
-t
,如下所示:
git checkout-t branchname

以后,可以使用
git switch
切换分支。

对我有效的方法如下:

git branch -a
git checkout -b <local branch name> <Remote branch name>
Example: git checkout -b Dev_8.4 remotes/gerrit/Dev_8.4
git branch
git pull
切换到所需的分支:

然后我把“主人”拉过来:


我用它来切换一个分支到另一个分支,任何人都可以使用它,它对我来说就像魅力一样

git交换机[branchName]或 git签出[branchName]

例如:git交换机开发或

git checkout开发日常生活中有用的命令:

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
如果要从功能分支合并到开发人员, 首先使用命令“git branch dev/develop”检查dev branch
然后输入merge commadn“git merge featurebranchname

更改分支的最佳命令

git branch -M YOUR_BRANCH

检查远程分支列表:

git branch -a
git checkout -b <local branch name> <Remote branch name>
Example: git checkout -b Dev_8.4 remotes/gerrit/Dev_8.4
git branch
git pull
切换到另一个分支:

git branch -a
git checkout -b <local branch name> <Remote branch name>
Example: git checkout -b Dev_8.4 remotes/gerrit/Dev_8.4
git branch
git pull
更新所有内容:

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

要切换到带有更改的分支,您应该首先进行提取。这是为了保存更改,如您的包.json.env文件

因此:
git fetch

然后:
git签出


这个答案适用于那些像我一样陷入困境一段时间的人。

这个答案是正确的(和往常一样,而且是经过投票的),但我会添加一条可能有用的评论:
git checkout
命令在我看来做了太多事情。这就是为什么这里有这么多的操作模式。如果git checkout所做的唯一一件事就是切换分支,那么答案很简单,但它也可以创建分支,甚至可以在不切换分支的情况下从特定提交中提取文件。这是正确的答案,但显示了git在命令行中是如何被搞砸的。git签出切换分支?@thang好吧,在2.23.0版中,这一点得到了修正:您现在可以使用
git switch
切换到分支。switch似乎不适用于此版本的git。在这个版本的git中,我使用什么来切换到不同的分支?C:\widget>git--version git version 2.11.0.windows.3 C:\widget>git-switch-master git:“switch”不是git命令。请参阅“git--help”。C:\widget>@John使用
git checkout
代替旧版本,后者也适用于现代版本。
git checkout[branch]
对于大多数遇到这个问题的用户,最后一个命令会让我进入分离状态。