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?
November 2021 Update: As of git version 2.27, you can now use git switch --orphan <new branch> to create an empty branch with no history. Unlike git checkout --orphan <new branch> , this branch won't have any files from your current branch (save for those which git doesn't track).
An empty repository cannot have a branch, branches are pointers to commits. So you first have to commit something in the empty repository before the branch can be created. You can either commit the code from the other repository, or just an empty file, create your branch and then commit the code.
The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.
To create a new local branch based on a remote branch, use the "-track" option in the branch command. You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With