How to Create Remote Git Branch
Branches in git help you to separate features from the main working branch. They come pretty handy in splitting the tasks into different branches. For example, suppose the production branch is named main
in which the team pushes changes. The team could create another branch, staging
, where they push changes, and the testing team could test the features from the staging
branch before pushing the changes to the main
branch.
However, the branches could be different in the local and remote repositories. If you divert from the main
branch to, for example, the dev
branch in a local machine to make some changes and push the local branch to a remote repository, then there are a few ways you can follow to do this effectively.
Create Remote Branch in Git
The cool thing about git is that when you push the locally created branch to the remote repository in git, the locally created branch is also pushed to the remote server. So, if you want to create a branch in a remote repository, you can start by creating a branch locally. You can do that using the following syntax.
git checkout -b <branch-name>
For example, if you want to create a branch named dev
you can do that by,
git checkout -b dev
Now, you can push the branch to the remote repository using the following command.
git push <remote-name> <branch-name>
The <remote-name>
here defaults to origin
, which points to the repository URL the project was cloned from.
Here, carrying on from our example, you can push the newly created dev
branch as,
git push origin dev
If however you want the remote branch name to be different than the local one, you can follow this syntax instead,
git push <remote_name> <local_branch_name>:<different_remote_branch_name_you_want>
If you mention just one name, it will assume the local_branch_name
and remote_branch_name
are the same. Now, other developers can easily pull the changes from the dev
branch to their local machine using the git pull origin dev
command.
If you want to update the dev
branch with the content from the main
branch, you can do git pull origin master
after checking out to the dev
branch (using git checkout dev
).