Using the slash character in Git branch name

后端 未结 6 963
孤城傲影
孤城傲影 2020-11-22 11:28

I\'m pretty sure I saw somewhere in a popular Git project the branches had a pattern like \"feature/xyz\".

However when I try to create a branch with the slash chara

相关标签:
6条回答
  • 2020-11-22 11:45

    just had the same issue, but i could not find the conflicting branch anymore.

    in my case the repo had and "foo" branch before, but not anymore and i tried to create and checkout "foo/bar" from remote. As i said "foo" did not exist anymore, but the issue persisted.

    In the end, the branch "foo" was still in the .git/config file, after deleting it everything was alright :)

    0 讨论(0)
  • 2020-11-22 11:49

    Are you sure branch labs does not already exist (as in this thread)?

    You can't have both a file, and a directory with the same name.

    You're trying to get git to do basically this:

    % cd .git/refs/heads
    % ls -l
    total 0
    -rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 labs
    -rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 master
    % mkdir labs
    mkdir: cannot create directory 'labs': File exists
    

    You're getting the equivalent of the "cannot create directory" error.
    When you have a branch with slashes in it, it gets stored as a directory hierarchy under .git/refs/heads.

    0 讨论(0)
  • 2020-11-22 12:00

    In my case, I forgot that there was already an unused labs branch. Deleting it solved the problem:

    git branch -d labs
    git checkout -b labs/feature
    

    Explanation:

    Each name can only be a parent branch or a normal branch, not both. Thats why the branches labs and labs/feature can't exists both at the same time.

    The reason for this behaviour is that the branches are stored in the file system and there you also can't have a file labs and a directory labs at the same level.

    0 讨论(0)
  • 2020-11-22 12:04

    I could be wrong, but I thought that slashes only appeared in branch names when they related to a remote repo, for example origin/master.

    0 讨论(0)
  • 2020-11-22 12:05

    It is possible to have hierarchical branch names (branch names with slash). For example in my repository I have such branch(es). One caveat is that you can't have both branch 'foo' and branch 'foo/bar' in repository.

    Your problem is not with creating branch with slash in name.

    $ git branch foo/bar
    error: unable to resolve reference refs/heads/labs/feature: Not a directory
    fatal: Failed to lock ref for update: Not a directory
    

    The above error message talks about 'labs/feature' branch, not 'foo/bar' (unless it is a mistake in copy'n'paste, i.e you edited parts of session). What is the result of git branch or git rev-parse --symbolic-full-name HEAD?

    0 讨论(0)
  • 2020-11-22 12:07

    Sometimes that problem occurs if you already have a branch with the base name.

    I tried this:

    git checkout -b features/aName origin/features/aName
    

    Unfortunately, I already had a branch named features, and I got the exception of the question asker.

    Removing the branch features resolved the problem, the above command worked.

    0 讨论(0)
提交回复
热议问题