Frequently asked questions around Git and Version Control.
How to Create a Remote Branch in Git
Actually, Git does not allow creating a (new, isolated) branch on a remote repository. Instead, you can push an existing local branch and thereby publish it on a remote repository.
In this short article, we'll explain how to do this and what to watch out for!
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
(1) A Local Branch You Want to Publish
As already said, creating a remote branch actually starts on the opposite end: in your local Git repository! You need to make sure you have a local branch that represents a state you want to push to the remote.
If you already have such a local branch at hand, you can simply check it out:
$ git checkout <branch-name>
If such a local branch doesn't yet exist, you can easily create it:
# To create a new local branch... # ...based on the current revision: $ git checkout -b <branch-name> # ...based on a specific revision hash: $ git checkout -b <branch-name> <commit-hash>
(2) Push the Local Branch to the Remote Repository
Now, with the correct local branch checked out, you can publish it on a remote repository - thereby "creating" it on that remote:
$ git push -u origin <branch-name>
Please mind the "-u" option: it establishes a "tracking relationship" between the existing local and the new remote branch.
The article "How to Set Upstream Branch in Git" explains this in detail. But here's a brief explanation: such a tracking relationship makes any future "push" and "pull" operations very easy. You can simply run a plain
git push or
git pull without any further options! The tracking relationship saved the source/target branch and the exact remote so that it can be looked up in further interactions.
Creating Remote Branches in Tower
In case you are using the Tower Git GUI, creating a remote branch is as easy as drag and drop: in the sidebar, simply drag the local branch you want to publish and then drop it onto the respective remote (probably "origin")!
- Check out the chapter on Inspecting Remote Data in our free online book.
- More frequently asked questions about Git & version control