Search code examples
gitgit-push

git create branch from hash with out fetching


I'm working on scripting some git commands for managing releases. One thing I would like to do if create a branch on the remote server using a hash on the remote server. But I'd like to run this without the overhead of having to fetch an entire tree. I get the hash from git ls-remote <url> then I want to push to the remote as well via something like git push <url> <hash>:refs/heads/release/1.0.0 but in order for this to work I need to init a repository, then do a fetch the hash.

I'm hoping there is a more direct way than having to fetch first. and I should note I'm hopefully optimizing the extra steps by initializing a bare repo, then doing a shallow fetch with depth=1.


Solution

  • Except for a very few commands git works only with the local repository; the limited set of subcommands is: clone, fetch, pull, push, ls-remote.

    So to work with remote repository you have 3 alternatives:

    1. Use an out-of-git API provided by the remote host. Examples are Github/Gitlab APIs.

    2. Connect to the remote host using ssh and work with the remote repository locally, directly at the server.

    3. Clone locally; fetch just enough to create and push a branch.

    The end of the list.