下面哪一行是正确的?
git checkout 'another_branch'
或者
git checkout origin 'another_branch'
或者
git checkout origin/'another_branch'
它们之间的区别是什么?
发布于 2017-12-04 18:26:03
如果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
那就是创建another_branch
来自origin/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
作为远程,是指向远程存储库的url的别名。
git checkout origin/another_branch
如果满足以下条件,则成功origin/another_branch
存在。它会导致处于分离的头状态,而不是在任何分支上。如果进行新的提交,则无法从任何现有分支访问新的提交,并且不会更新任何分支。
更新
由于2.23.0已经发布,有了它,我们也可以使用git switch
若要创建和切换分支,请执行以下操作。
如果foo
存在,请尝试切换到foo
git switch foo
如果foo
不存在并且origin/foo
已存在,请尝试创建foo
来自origin/foo
然后切换到foo
git switch -c foo origin/foo
# or simply
git switch foo
更一般地,如果foo
不存在,请尝试创建foo
从已知的引用或提交,然后切换到foo
git switch -c foo
git switch -c foo
如果我们同时在Gitlab和Github中维护一个存储库,则本地存储库可能有两个远程,例如,origin
对于Gitlab和github
为了Github。在这种情况下,存储库具有origin/foo
和github/foo
。
git switch foo
会抱怨fatal: invalid reference: foo
,因为它不知道来自哪个ref,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
存在,请尝试重新创建/force-createfoo
从(或重置foo
to)已知的引用或提交,然后切换到foo
git switch -C foo
git switch -C foo
它们等同于:
git switch foo
git reset [|] --hard
尝试切换到已知引用或提交的分离标头:
git switch -d
git switch -d
如果您只想创建一个分支,而不想切换到它,请使用git branch
取而代之的是。尝试从已知的引用或提交创建分支:
git branch foo
git branch foo
发布于 2017-12-04 19:23:19
切换到git中的另一个分支。答案很简单,
git-checkout -切换分支或恢复工作树文件
git fetch origin <----this will fetch the branch
git checkout branch_name <--- Switching the branch
在切换分支之前,请确保您没有任何修改过的文件,在这种情况下,您可以提交更改,也可以将其隐藏起来。
发布于 2020-03-14 04:44:26
日常生活中有用的命令:
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,请首先使用以下命令检查dev分支:git分支开发/开发“然后输入merge commadn”git合并功能分支名称“
https://stackoverflow.com/questions/47630950
复制相似问题