Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Create branch in remote repository without having local repository




Is it possible to make a branch in a remote master repository without having a local clone of the repository?

The repository is large enough that cloning a copy just to make and push the branch is extremely wasteful (about 20 minutes), especially as the branch is being made by an automated system for procedural and functional reasons, and it will not need the contents of the repository itself.

like image 647
simpleuser Avatar asked Mar 22 '23 19:03


2 Answers

Not based on existing branches but you can push in an empty branch using a completely new unrelated repository. Try out the following test instructions

Creating the original repo for the test

cd ~/Desktop/test
mkdir original_repo && cd original_repo 
git init && touch testfile && git add . && git commit -m "master repo"
cd ..

Create a new empty repo

mkdir hacky_repo && cd hacky_repo
git init && touch init && git add . && git commit -m "empty new repo"

Add the original repo as a remote to this repo

git remote add original ../original_repo/

And now push the branch from this repo to the orignal repo

git push original master:some_new_branch
cd ../original_repo

If now you do a git branch - you should have the new branch there in your original repo.

$) git branch
* master

The assumption here is that you know in advance which all branches / branch namespace are free.

like image 70
Anshul Goyal Avatar answered Apr 25 '23 07:04

Anshul Goyal

In addition of pushing a branch from any repo, other possibilities include setup on the server some kind of listener in order to:

  • Trigger the creation of a branch (http listener)
  • Make the branch on the remote side (command through ssh)

But that supposes you have access to that server.

If the server is GitHub, you wouldn't be able to do that, but you would have the GitHub API to work with, like "Create a reference".

like image 44
VonC Avatar answered Apr 25 '23 07:04