Can't checkout git branch started with hyphen (-)

匿名 (未验证) 提交于 2019-12-03 08:39:56

问题:

I have cloned a git repository.
This repository has a remote branch something like -feature-abc.
When I type git checkout -feature-abc, I get:

error: unknown switch `e'

Any idea how to checkout this branch?

回答1:

Try using this syntax:

git checkout -- -feature-abc

The double hyphen syntax should help git separate the command options from the actual branch name parameter, as I explained before in "Deleting a badly named git branch".


If that doesn't work, you can follow the recipe suggested in "How do I rename a local Git branch?"

Go into your working copy's .git/refs/heads, find the filename "-dumb-name", get the hash of the branch. Then this will check it out, make a new branch with a sane name, and delete the old one.

  • Go into your working copy's .git/refs/heads,
  • find the file named "-feature-abc",
  • get the hash of the branch (cat the file).
  • Then check it out, make a new branch with a sane name, and delete the old one.
git checkout {hash} git checkout -b brilliant-name git branch -d -- -dumb-name


回答2:

Finally I am able to get things working based on VonC's Answer.

Solution 1:

git checkout -b feature-abc origin/-feature-abc

Solution 2:

  • Go into your working copy's .git/refs/remotes/origin,
  • find the file named "-feature-abc",
  • get the hash of the branch (cat the file),
  • Then check it out, make a new branch with a sane name,
  • Make new branch track remote branch.
git checkout {hash} git checkout -b feature-abc git branch --set-upstream-to=origin/-feature-abc feature-abc

Make an existing Git branch track a remote branch?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!