这些句子中哪一个是正确的?
git checkout 'another_branch'
或者git checkout origin 'another_branch'
或者git checkout origin/'another_branch'
他们之间有什么区别?
这些句子中哪一个是正确的?
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_branch
设置为another_branch
的上游。git checkout another_branch
会返回错误。git checkout origin another_branch
会返回错误。如果origin
是一个版本,而another_branch
是一个文件,则它会检出该版本的文件,但大多数情况下这不是你想要的。 origin
主要用于git fetch
、git pull
和git push
中作为远程仓库的别名。origin/another_branch
存在,则git checkout origin/another_branch
成功。这将导致进入脱离HEAD状态,没有任何分支。如果你做了新的提交,那么这些新的提交无法从任何现有的分支中找到,并且不会更新任何分支。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/foo
和github/foo
。 git switch foo
将报错fatal: invalid reference: foo
,因为它不知道从哪个引用位置(origin/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
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>
git checkout
命令做了太多的事情。这就是为什么这里有这么多操作模式。如果 git checkout
唯一要做的就是切换分支,那么问题就简单了,但它还可以创建分支,甚至可以从特定提交中提取文件而不需要切换分支。 - torekgit switch
命令来切换到一个分支,这个问题得到了解决。 - legends2kgit checkout
,这个命令在现代版本中同样有效。 - ElpieKay在git中切换到另一个分支。简单明了的答案,
git-checkout - 切换分支或还原工作树文件
git fetch origin <----this will fetch the branch
git checkout branch_name <--- Switching the branch
在切换分支之前,请确保您没有任何修改过的文件,如果有的话,可以提交更改或将其隐藏。
日常工作中有用的命令:
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”。
[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 switch <branch name>
来切换分支。git restore
命令来代替 checkout 操作。 - Default我成功的方法如下:
切换到需要的分支:
git checkout -b BranchName
git pull origin master
-t
,像这样:git checkout -t 分支名称
。检查: git branch -a
如果只显示一个分支,请按以下步骤操作。
git config --list
git config --unset remote.origin.fetch
git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git switch -t origin/branchname
之前,这就是我需要做的事情。我不太确定最后一个命令做了什么,但似乎我的 remote.origin.fetch
只查看了 +refs/heads/current_branch:refs/remotes/origin/current_branch
,因此不知道其他分支的存在(之前我一直收到错误 fatal: invalid reference: origin/branchname
)。 - b4tchgit fetch
git checkout <new branch>
此答案适用于那些像我一样遇到问题的人。
使用单个命令可以切换到git中的另一个分支。
git switch 分支名称
git checkout [branch]
这个命令对于大多数使用者来说可能比较常见。 - JGallardogit switch branch-name
,这是一种新的方法。再次强调,git checkout branch-name
仍然被支持并且可用。 - Meet Bhalodiya