Search code examples
gitgit-branchgit-bare

How can I create a branch on a bare repo in Git?


There are a dozen similar questions, with everyone posting long answers trying to talk the asker out of it.

Is it possible to create a branch in such a situation?

I do not want to clone the repo, just so that I can create a branch in that copy's working directory, push it, and then delete the whole thing again. The script that will create this branch (after checking to see if it exists) already has access to the bare repo in Gitolite and can issue the commands directly. "git checkout" complains that the operation must be performed in a work tree.


Solution

  • You can just go a little lower-level, in a bare repo.

    git branch is willing to create a new branch in a bare repo. Just give it a commit to start from and you've made a new branch name pointing at an existing commit:

    cd foo.git
    git branch newbranch b6636ec88ba0750aec2706865653eb55031fb892
    

    You can also use git update-ref, which is even lower-level than git branch and can create references outside of the refs/heads/ namespace:

    git update-ref refs/special/ziggy b6636ec88ba0750aec2706865653eb55031fb892
    git update-ref -d refs/special/delete
    

    Edit: And, you can make indirect refs with git symbolic-ref, e.g.:

    git symbolic-ref MAGIC refs/tags/v1.3
    

    (normally this would only be used to make HEAD be a different indirect ref; the main point here is that this command, like git update-ref, requires that you spell out the whole name-space name).