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

How can I switch to another branch in git?

以下哪一句是正确的?

git checkout 'another_branch'

git checkout origin 'another_branch'

git checkout origin/'another_branch'

它们之间有什么区别?

如果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_branchanother_branch的上游。

如果两者都不存在,git checkout another_branch returns 错误。

大多数情况下

git checkout origin another_branch returns 错误。如果 origin 是一个修订版并且 another_branch 是一个文件,那么它会检出该修订版的文件,但这很可能不是您所期望的。 origin 主要用于 git fetchgit pullgit push 作为远程,url 到远程存储库的别名。

如果 origin/another_branch 存在,

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

更新:

随着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维护一个仓库,那么本地仓库可能有两个remote,比如Gitlab的origingithub 98=]。在这种情况下,存储库有 origin/foogithub/foogit switch foo 会抱怨 fatal: invalid reference: foo,因为它不知道从哪个 ref,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 存在,尝试 recreate/force-create foo 从(或重置 foo 到)一个已知的引用或提交然后切换到 foo :

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

相当于:

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

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

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

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

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

[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 中的另一个分支。直截了当的回答,

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

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

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

检查: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/*

如果您希望分支跟踪远程分支,这在您要向分支提交更改和拉取更改等时非常重要,您需要为实际结帐添加一个 -t像这样: git checkout -t branchname

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

对我有用的是以下内容:

Switch to the needed branch:

git checkout -b BranchName

And then I pulled the "master" by:

git pull origin master

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

git 切换 [分支名称] 或 git结账[分行名称]

例如:git 切换开发或
git结帐开发

日常生活中有用的命令:

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

如果你想从feature分支合并到dev, 首先使用命令“git branch dev/develop”检查开发分支 然后输入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

要用您的更改切换到一个分支,您应该先进行提取。这是为了保存 package.json.env[=27 之类的更改=] 个文件

所以:
git fetch

然后:
git checkout <new branch>

这个答案是给那些像我一样卡住了一段时间的人的。

这些是我遵循的步骤:

  • git克隆{link}
  • cd {repo 文件夹}

您可以查看状态和您正在使用的分支:

  • git状态
  • git 分支
  • git分支-a

注意:如果您在移动到新分支之前在本地仓库中进行了更改,则以下步骤应该仍然有效。

如果“git branch”显示master,你想创建+移动到另一个分支:

  • git checkout -b {分行名称}

使用“git分支”再次检查分支 现在应该显示您在新分支中。

现在添加、提交和推送:

  • git 添加 .
  • git commit -m “添加新分支”
  • git push origin {分支名称}

以上步骤适用于我在移动到新的本地分支之前进行更改或在移动到新分支之后进行更改的情况。 我希望它能帮助人们 运行 遇到类似的情况,它也是这里提到的问题的解决方案:Link

切换到 git 中的另一个分支可以通过一个命令完成。

git switch branch-name