git create remote branch only (without local)

This question already has an answer here:

  • How to create a remote Git branch without creating a local branch?

    1 answer

  • How to authenticate LDAP connection on Git (gogs)?
  • Check if directory is a Git root in one command
  • OS X Mavericks: Git bash prompt has stopped tracking if uncommitted changes are present
  • How do I create a git hook to prepend commit messages with branch name?
  • Problems using git diff to create file list for deploy
  • Zip latest committed changes only
  • What's the difference between “git symbolic-ref HEAD refs/heads/gh-pages” and “git branch gh-pages”?
  • How do I set the git username and password when using gitpython?
  • Permission denied although I can do whoami
  • .gitignore still tracks file even after removing cache
  • HEAD position in the example from git docs on stash command
  • Should I delete the LOCAL remote/origin/master branch after I have merged it with my local master?
  • One Solution collect form web for “git create remote branch only (without local)”

    AFAIK, this isn’t possible. However, I would use alternative solutions (in ascending order):

    1. If your client’s security policy allows it, make some api to the remote machine that would create a new branch, e.g. an HTTP PUT [*] request with authentication and the name of the new branch as a parameter. Then, you can access it via curl or your favorite language, without needing a full-fledged ssh access.

    2. Create an empty branch, so cloning will be very fast, even if the repo is large. E.g.:

      git checkout -b empty
      git rm *
      git commit -m"create an empty branch"
      git push -u origin empty

      You need to do it only once per repo. Then, when cloning, you can clone just this branch using the -b switch. E.g.:

      git clone -b empty --depth 1 <repo>

      Note that --depth implies --single-branch.

      Now you can create a new branch with the name you want:

      git checkout -b <new-branch>

      After a developer finishes working on a feature, the new branch can be updated by using the -f switch:

      git push -f origin <local-branch>:<new-branch>
    3. Use a git hook on the client-side (i.e. the developer’s side), that will enforce the best practices, e.g. pre-push. Assuming, of course, that your co-workers won’t ignore it 🙂

    [*] or POST, but since you’re providing the name of the branch, it should be idempotent.

    Git Baby is a git and github fan, let's start git clone.