Git is a powerful version control system you can use on virtually any workstation, but it is recommended that you set up a central hub for your project on your own private server. In this article, you will learn how to push a local branch in your Git project up to the remote server. This is not only a good way to back up your branch to the cloud, but also improves collaboration between other users.
- Create Your Own Private Git Server
- Why Use Remote Branches?
- Create a New Branch Locally
- Push New Branch To Remote and Set as Upstream
Create Your Own Private Git Server
In order to get the most out of this article, you will need to have your own Git server where you can manage your Git projects. For a few bucks a month you can easily create a private Git server on your own cloud VPS. This gives you increased security, control, and options. However, if you just want to try Git out for free, the tips contained in this article will work similarly on free Git hosting sites.
Why Use Remote Branches?
If you just want to know how to push a branch to your remote you can skip ahead to that section.
In order to understand how all of the Git architecture works together, you will get a quick review here about creating branches in Git. Git branches allow you to divert from the “master” branch and work on your project separately. You can then “merge” any new changes into the master branch.
Branches Of The Tree
You can think of these divergent pathways like the branches of a tree. For example, you have your master branch, but you might create a divergent branch called “testing.” From the “testing” you can have unlimited divergent branches for specific purposes, and all of those branches can be merged back into the “testing” branch. Meanwhile, your master files are untouched.
To get the most use out of branches you will want to make sure that a local branch is tracking changes in a remote branch. This is useful for lots of reasons. For example, your local master branch is tracking the remote master for changes. But imagine you have a local branch called “staging” where you test changes before merging them into master. Now imagine your local workstation suffers a catastrophic disk failure. Your master branch is safe because you recently pushed changes to master. But the “staging” branch is lost.
Ease of Using Divergent Branches For Redundancy and Collaboration
In this article, you will learn how to push up your divergent branches so you can track changes remotely. This will not only protect you from loss of data but also help with collaboration between other users in your organization. Your remote Git server acts as a central hub for different users to submit data.
As you would have seen in the article linked above about creating your own private Git server, you set up a remote repository by adding the server location and file path to your local configuration:
git remote add origin [email protected]:/path/to/project.git
- The name of the remote repository can be anything you want, but “origin” is a popular choice.
- [email protected]:/path/to/project.git
- Git is a common username for interacting with your server. This part of the SSH syntax that will be used to communicate with the server, followed by the file path to the bare Git repository.
Create a New Branch Locally
To create a new local branch and switch to it, you can use the checkout command. For this example, we will presume you want to name the new branch “staging”:
git checkout -b staging
You now have a new branch called “staging” and you can view all branches with the
git branch command as below:
Push New Branch To Remote and Set as Upstream
Now that you have a new branch in your local configuration, it only requires a few commands to send that branch up to the server. The Git
push command is the first one you will want to run, effectively placing your new branch on the remote repository:
git push origin staging
Then, while the branch is still checked out, you can set it an “upstream” branch for it, which means the local branch will be tracking its remote counterpart for changes instead of the master branch:
git push origin -u staging
You will see a success message indicating that the branch is now set up for tracking. That’s it! You now know how to push local branches to the cloud.