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

409

这些句子中哪一个是正确的?

git checkout 'another_branch'
或者
git checkout origin 'another_branch'
或者
git checkout origin/'another_branch'

他们之间有什么区别?


81
git checkout [branch] 这个命令对于大多数使用者来说可能比较常见。 - JGallardo
4
这是一个快速的答案:git switch branch-name,这是一种新的方法。再次强调,git checkout branch-name仍然被支持并且可用。 - Meet Bhalodiya
13个回答

406
如果本地已经存在分支another_branch,而你不在这个分支上,则git checkout 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。这将从origin/another_branch创建分支another_branch并将origin/another_branch设置为another_branch的上游。
如果两者都不存在,则git checkout another_branch会返回错误。
大多数情况下,git checkout origin another_branch会返回错误。如果origin是一个版本,而another_branch是一个文件,则它会检出该版本的文件,但大多数情况下这不是你想要的。 origin主要用于git fetchgit pullgit push中作为远程仓库的别名。
如果origin/another_branch存在,则git checkout origin/another_branch成功。这将导致进入脱离HEAD状态,没有任何分支。如果你做了新的提交,那么这些新的提交无法从任何现有的分支中找到,并且不会更新任何分支。
更新:随着2.23.0的发布,我们还可以使用git switch创建和切换分支。如果foo存在,则尝试切换到foo
git switch foo

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

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

更一般地,如果foo不存在,请尝试从已知的引用或提交创建foo,然后切换到foo

git switch -c foo <ref>
git switch -c foo <commit>

如果我们同时在GitLab和GitHub中维护一个仓库,本地仓库可能会有两个远程,例如origin用于GitLab,github用于GitHub。在这种情况下,该仓库将具有origin/foogithub/foogit switch foo将报错fatal: invalid reference: foo,因为它不知道从哪个引用位置(origin/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

如果存在 foo,则尝试从已知的 ref 或 commit 重新创建 / 强制创建 foo 并切换到 foo
git switch -C foo <ref>
git switch -C foo <commit>

等同于以下内容:

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

尝试切换到已知 ref 或 commit 的分离 HEAD:

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

如果你只想创建一个分支但不切换到它,可以使用 git branch 命令。尝试从已知的 ref 或 commit 创建分支:

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

33
这个答案是正确的(像往常一样,并且被点赞了),但我想补充一句评论,可能会有帮助:在我看来,git checkout 命令做了太多的事情。这就是为什么这里有这么多操作模式。如果 git checkout 唯一要做的就是切换分支,那么问题就简单了,但它还可以创建分支,甚至可以从特定提交中提取文件而不需要切换分支。 - torek
13
这是正确的答案,但显示了命令行中Git存在某些问题。git checkout 用于切换分支? - thang
4
从2.23.0版本开始,你可以使用git switch命令来切换到一个分支,这个问题得到了解决。 - legends2k
这个版本的git好像无法使用switch命令。在这个版本的git中,我该使用什么来切换到另一个分支呢? C:\widget>git --version git version 2.11.0.windows.3C:\widget>git switch master git: 'switch' 不是 git 命令。请参阅 'git --help'。 C:\widget> - John
2
@John 在旧版本中请使用git checkout,这个命令在现代版本中同样有效。 - ElpieKay
我对这个问题的许多答案感到困惑。如果我已经检出了一个分支b1,那么检出另一个分支b2会替换b1的文件吗(在它们不同的地方)?如果不是,那么我该如何将b2的文件实际放入我的文件夹中? - Motorhead

92

在git中切换到另一个分支。简单明了的答案,

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

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

在切换分支之前,请确保您没有任何修改过的文件,如果有的话,可以提交更改或将其隐藏。


最后一个命令将我置于“分离 HEAD”状态,这意味着无法编辑该分支。 - user8434768
2
你正在尝试检出的分支未被获取,因此在检出之前需要获取。如果该分支是最新的,则可以跳过获取步骤,直接使用git checkout branchname命令即可。 - danglingpointer
在切换到分支后执行“git pull”不就足够了吗? - user8434768
拉取(pull)也可以,它会在后台执行获取(fetch)和合并(merge)操作。我看不出有什么区别。 - danglingpointer

46

日常工作中有用的命令:

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

如果你想从功能分支合并到dev,请先使用命令“git branch dev/develop”检出dev分支,然后输入合并命令“git merge featurebranchname”。


谢谢兄弟,简洁是才华的姐妹。请回复上面的内容。 - Qui-Gon Jinn
简单而实用的指南。非常感谢! - undefined

22

[git checkout "branch_name"]

这是另一种说法:

[git checkout -b branch_name origin/branch_name]

如果"branch_name"仅存在于远程上。

[git checkout -b branch_name origin/branch_name] 在拥有多个远程时非常有用。

关于 [git checkout origin 'another_branch'] 我不确定是否可行,据我所知,您可以使用 "fetch" 命令来实现此操作 -- [git fetch origin 'another_branch']


我知道使用命令“git checkout -b branchName”创建另一个分支。但这不是问题的关键! - user8434768

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

1
天啊,这真是个革命性的变化。稍微相关一点,你可以使用 git restore 命令来代替 checkout 操作。 - Default

9
以下是我的做法:

我成功的方法如下:

切换到需要的分支:

git checkout -b BranchName

然后我通过以下方式获取"master":
git pull origin master

6
如果你想让分支跟踪远程分支,这非常重要,因为你会向此分支提交更改并拉取更改等操作,你需要在实际检出时添加-t,像这样:git checkout -t 分支名称

6

检查: git branch -a

如果只显示一个分支,请按以下步骤操作。

  • 步骤1: git config --list
  • 步骤2: git config --unset remote.origin.fetch
  • 步骤3: git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

2
我想知道这一系列的命令怎样才能切换到另一个分支。 - user8434768
当您之前使用深度参数进行浅克隆时,现在想知道为什么不能获取其他远程分支的情况下,执行上述建议的命令仍然出现“错误:pathspec 'another_branch' did not match any file(s) known to git”时,可以尝试执行此操作。这与原始问题无关,但可以帮助其他人解决类似问题。 - luciash d' being
在运行 git switch -t origin/branchname 之前,这就是我需要做的事情。我不太确定最后一个命令做了什么,但似乎我的 remote.origin.fetch 只查看了 +refs/heads/current_branch:refs/remotes/origin/current_branch,因此不知道其他分支的存在(之前我一直收到错误 fatal: invalid reference: origin/branchname)。 - b4tch

3
为了在分支中切换你的更改,你应该先执行一次fetch操作。这样可以保存像package.json.env文件这样的更改。
所以:
git fetch
然后:
git checkout <new branch> 此答案适用于那些像我一样遇到问题的人。

2

使用单个命令可以切换到git中的另一个分支。

git switch 分支名称


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接