33

Most examples of creating remote branches involve pushing from a local branch

Is there a way of creating an empty remote branch without pushing?

Is it also possible to create a local empty branch,check it out then link it to the new also empty remote branch without pushing?

vfclists
  • 19,193
  • 21
  • 73
  • 92
  • 2
    Why don't you want to push, keeping in mind that pushing doesn't require sending any commits? And what do you mean by an "empty" branch? – Evan Shaw Apr 04 '10 at 12:41
  • 1
    How could it be a *remote branch* without the "remote" even knowing about it? – ndim Apr 04 '10 at 12:49

3 Answers3

30

As mentioned in the blog post "Start a New Branch on your Remote Git Repository":

  • Creating a Remote Branch
git push origin origin:refs/heads/new_feature_name
  • Make sure everything is up-to-date
git fetch origin
  • Then you can see that the branch is created.
git branch -r

This should show ‘origin/new_feature_name

  • Start tracking the new branch
git checkout --track -b new_feature_name origin/new_feature_name

So to declare a remote branch, even one which doesn't yet exist on the local repository, git push is mandatory.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks VonC, this is what I need to understand. In effect there is no hierachical structure as far as Git is concerned. The remote is no different from the local other than controls on pulling and pushing that the managers of the remote will put in place – vfclists Apr 07 '10 at 07:37
14
git checkout --orphan new-empty-branch

Then

git rm -rf .

to remove all files from new branch.

Onlyjob
  • 5,692
  • 2
  • 35
  • 35
1

It's worth pointing out that there is no such thing as an empty branch in Git. A branch name, in Git, always selects some particular commit. That commit is, by definition, the last commit on that branch. So a branch always has at least one commit on it.

What's unusual about Git, compared to other version control systems at least, is that the commit that is the last commit on any one particular branch, can also be on some other branch(es) at the same time. It can be the last commit on more than one other branch, and it can be a non-last commit on some other branch(es).

VonC's answer is usually the right one for those who are asking this question.

Onlyjob's answer is the right way to set things up to create a new branch that's not empty but that does not share any commits with any other existing branches. That's a rare thing to want to do, but when that is what you want, creating a new branch name some other way won't work.

torek
  • 448,244
  • 59
  • 642
  • 775
  • No empty branches indeed. But there is the concept of "unborn" branch, for which HEAD does not exist. I suspect a `git switch --orphan newBranch` would be considered unborn. – VonC Jan 08 '21 at 23:53
  • @VonC: right, that's Onlyjob's answer. The `--orphan` checkout (or switch) sets you up for that, and the eventual `git commit` creates a new commit that is a root commit. – torek Jan 09 '21 at 00:24