13

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?

chanchal118
  • 3,551
  • 2
  • 26
  • 52

2 Answers2

14

Try using this syntax:

git checkout -- -feature-abc

Or, since August 2019, Git 2.23 and git switch presented here:

git switch -- -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

    or

    git switch -c brilliant-name git branch -d -- -dumb-name

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Solution 1 using '--' in checkout is not working. It gives error "error: pathspec '-feature-tipslist' did not match any file(s) known to git." – chanchal118 Dec 07 '13 at 14:13
  • Solution 2 will not also work. There is no file name '-feature-abc' inside '.git/refs/heads' as i have no able to checkout that branch yet. – chanchal118 Dec 07 '13 at 14:23
9

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?

Community
  • 1
  • 1
chanchal118
  • 3,551
  • 2
  • 26
  • 52
  • 2
    You didn't mention that this branch was a remote one. My solutions would have worked with `origin/-feature-abc` instead of `-feature-abc`. But +1 nonetheless. – VonC Dec 07 '13 at 15:47