下面哪一行是正确的?

git checkout 'another_branch'

or

git checkout origin 'another_branch'

or

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_branch设置为another_branch的上游。

如果两者都不存在,git checkout another_branch将返回错误。

Git签出源another_branch在大多数情况下返回错误。如果origin是一个修订版本,而another_branch是一个文件,那么它会检出该修订版本的文件,但这很可能不是您所期望的。Origin主要用于git fetch, git pull和git push中,作为远程存储库url的别名。

如果origin/another_branch存在,Git签出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不存在,尝试从已知的ref或commit创建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:

git switch -c gitlab_foo origin/foo
git switch -c github_foo github/foo

如果foo存在,尝试从(或重置foo到)一个已知的ref重新创建/强制创建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分支。尝试从已知的ref或commit创建一个分支:

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'],我不确定这是可能的,AFAK你可以使用“fetch”命令来做到这一点 ——[git获取来源'another_branch']


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

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

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

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


检查:git分支-a

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

步骤1:git配置——list 步骤2:git配置——unset remote.origin.fetch 步骤3:git配置——添加remote.origin.fetch +refs/heads/*:refs/remotes/origin/*


如果你想让分支跟踪远程分支,如果你要向分支提交更改和拉取更改等,这是非常重要的,你需要为实际的签出添加一个-t,如下所示: Git checkout -t branchname


在Git 2.23以后,可以使用Git switch <分支名称>来切换分支。


对我有效的方法如下:

切换到所需的分支:

git checkout -b BranchName

然后我把"大师"拉过来

git pull origin master

我用这个来切换一个分支到另一个你可以使用它为我工作的魅力。

git开关[branch]或 git结帐[branch]

示例:git switch develop OR 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

如果你想从特性分支合并到开发, 首先使用命令“git branch dev/develop”检查dev分支 然后输入合并命令"git merge featurebranchname"


检查远程分支列表:

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获取

然后: Git checkout <新分支>

这个答案是给那些被困住一段时间的人的,比如我。


以下是我遵循的步骤:

Git克隆{link} CD(回收文件夹)

你可以检查状态和你正在使用的分支:

git状态 git分支 Git分支-a

注意:在这里,如果您在迁移到新分支之前对本地回购进行了更改,那么以下步骤仍然有效。

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

Git checkout -b{分支名称}

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

现在添加、提交和推送:

Git添加。 Git commit -m "添加新分支" Git推送源{分支名称}

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


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

Git开关branch-name