How to Create a Remote Branch in Git
Branching is an efficient way of code management in any version control system. This snippet will help you create a remote branch in Git.
Steps to creating a remote branch
Now let’s see what you should consider in order to create a new remote branch:
Creating a local branch and switching to it
You must start by creating a local branch using the git checkout command as follows:
git checkout -b <new-branch-name>
It will create a new branch from your current branch. In case you want to create a new one from a different branch, you should indicate your branch name as the last argument of the command.
git checkout -b <new-branch-name> <from-branch-name>
Pushing a local branch to remote
You can work locally on the branch and push it whenever you are ready to share it. Push the branch to a remote repository by running the following command (suppose, the name of remote is origin, which is by default):
git push -u origin <branch-name>
Now, whenever your team members need to reach your branch, they can run the git fetch command as follows:
git fetch git checkout <branch-name>
There is an alternative solution for Git 2.0 or above.
It can be done with the help of the push.default configuration, by setting it to current in the following way:
git config --global push.default current
Afterwards, you can push a new branch with the command below:
git push -u
Push.default is defining what action git push must take in case a refspec is not given.
The git branch Command
The git branch is available in most of the version control systems. Branches are an essential part of the everyday development process in Git. The branches represent a pointer to a snapshot of a developer’s changes. Whenever developers want to fix bugs or add new features, they create a new branch. This gives an opportunity to clean up the future’s history before merging it to the main branch.
The git branch command will help you create, list, rename, or delete branches.
The git checkout Command
As it was stated above, git branch is targeted at creating, renaming, and deleting branches. But it doesn’t allow switching between branches. You can do it using the git checkout command. These two commands work together. The git checkout is also associated with another command - git clone. The latter is used for fetching code from a remote repository. At the same time, git checkout switches code versions on your local system.
The git push Command
The git push is generally used for uploading the local repository content to the remote. This command is targeted at publishing your upload local changes to the central repository. Whenever you make changes in your local repository, and you want to share them with other members of the team, you can use the git push command.