git checkout master 不切换分支 - 存储库损坏了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34287722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git checkout master does not switch branch - repository broken?
提问by schrom
tl;dr: git checkout master
does not switch to master branch, gives no error or any output at all and I have no clue why. Any other branch works fine.
tl;dr:git checkout master
不切换到 master 分支,根本没有给出错误或任何输出,我不知道为什么。任何其他分支都可以正常工作。
I have a git repository that consists of the branches development
and master
. I do a fresh clone of the repository, checking out branch development
as default.
我有一个 git 存储库,其中包含分支development
和master
. 我做了一个新的存储库克隆,development
默认检查分支。
$ git clone <REPO-URL> --branch development
$ git branch -a
* development
origin/HEAD -> origin/development
origin/development
origin/master
$ git show-ref
656c781c2affc26792f857baf8e232de07101535 refs/heads/development
656c781c2affc26792f857baf8e232de07101535 refs/remotes/origin/HEAD
656c781c2affc26792f857baf8e232de07101535 refs/remotes/origin/development
cfee1a1761642453edf5d001565f23b50243ff09 refs/remotes/origin/master
The ref master is pointing to is correct, that commit does exists and is the latest commit of my master branch.
ref master 指向的是正确的,该提交确实存在并且是我的 master 分支的最新提交。
So far everything looks normal, but when I try to switch to master this is happening:
到目前为止,一切看起来都很正常,但是当我尝试切换到 master 时,发生了这种情况:
$ git checkout master
$ git branch
* development
No message from checkout, no error, nothing, and the branch is not switched.
结帐时没有消息,没有错误,什么都没有,并且分支没有切换。
Things I have tried so far:
到目前为止我尝试过的事情:
- Creating a second branch
master2
from the same commit => checkout works fine. - Deleting and re-creating branch master both local and on origin => again no checkout possible
git checkout -b master --track origin/master
=> works, but I think it should not be necessary to use this command as this is whatgit checkout master
should do automatically- Committing and pushing to master works, but does not change the checkout problem
- I tried several git versions (1.9, 2.2) and machines (linux, windows), problem occurs everywhere.
master2
从同一个提交创建第二个分支=> checkout 工作正常。- 删除并重新创建本地和源上的分支 master => 再次无法结帐
git checkout -b master --track origin/master
=> 有效,但我认为没有必要使用此命令,因为这是git checkout master
应该自动执行的操作- 提交并推送到 master 工作,但不会改变结帐问题
- 我尝试了几个 git 版本(1.9、2.2)和机器(linux、windows),问题无处不在。
Any further ideas? What am I missing? Is my repository broken? How can I fix this?
任何进一步的想法?我错过了什么?我的存储库坏了吗?我怎样才能解决这个问题?
采纳答案by fhopf
I have seen something similar when there is a folder master in the source tree. Unfortunately I didn't find a way to tell git to interpret the value as a branch. Renaming the folder fixed the problem for me.
当源树中有一个文件夹 master 时,我看到了类似的东西。不幸的是,我没有找到告诉 git 将值解释为分支的方法。重命名文件夹为我解决了这个问题。
回答by Nick
If your repo has a folder or file name the same as a branch name then you will need to: git checkout xyz --
with the extra --
at the end. It tells git to use a branch or commit instead of trying to use a folder/file name.
如果你的仓库有一个与分支名称相同的文件夹或文件名,那么你需要:在末尾git checkout xyz --
加上额外--
的。它告诉 git 使用分支或提交,而不是尝试使用文件夹/文件名。
Found the answer on another stackoverflow post: Git change branch when file of same name is present
在另一个 stackoverflow 帖子中找到答案:Git change branch whenthe same name is present
回答by Bustikiller
The local master branch in your repository is not different from any other local branch in your repo. You cloned your repository to your development branch, which is the only local branch you have. Thus, if you try to checkout to your local master branch, git says it does not exist.
存储库中的本地主分支与存储库中的任何其他本地分支没有区别。您将存储库克隆到您的开发分支,这是您拥有的唯一本地分支。因此,如果您尝试结帐到本地 master 分支,git 会说它不存在。
If you want to have both development and master local branch initially the same, you can do one of these things:
如果您想让 development 和 master 本地分支最初相同,您可以执行以下操作之一:
Download your repo to master branch, and create a local development branch:
将你的 repo 下载到 master 分支,并创建一个本地开发分支:
git clone <repo_url>
git checkout -b development
or download your repo to development branch, and create a local master branch:
或者将你的 repo 下载到 development 分支,并创建一个本地 master 分支:
git clone <REPO-URL> --branch development
git checkout -b master
回答by BobbyA
The accepted answer (renaming the folder) works. If you don't want to do that, here's a work-around that I tested on git version 2.14.1.windows.1
.
接受的答案(重命名文件夹)有效。如果您不想这样做,这是我在git version 2.14.1.windows.1
.
Delete the offending folder.
删除有问题的文件夹。
perform git branch <the branch you had trouble switching to>
履行 git branch <the branch you had trouble switching to>
perform git branch <your original branch>
履行 git branch <your original branch>
perform git checkout -- .
to undo the deletion.
执行git checkout -- .
以撤消删除。
Now you can switch to and from your desired branch without issue, even with a with a folder in the repo that's named the same as a branch. You'll notice that the branch appears in the branch list if you perform git branch
现在,您可以毫无问题地切换到您想要的分支,即使存储库中有一个与分支同名的文件夹。如果您执行,您会注意到分支出现在分支列表中git branch