How to Add Remote Branch in Git

Abdul Jabbar Feb 02, 2024
  1. Git Remote
  2. Add a Remote Repository in Git
How to Add Remote Branch in Git

Git does not permit its developers to create a new branch on a remote repository. But instead of it, we can push an already existing local branch, and after doing so, we can bring it to a remote repository using some Git commands.

In every version control system, branching is considered the best way of code management and helps us create a remote branch in Git. Branching is used in development operations every day to separate the business-related changes from each other’s code, or some developers prefer to create their branches to develop any new requirements or features.

When we are in need to add a new item or feature in the code development, or we want to fix a bug that has been noticed by the client or the quality assurance person in the team, then we will generate a new branch in the same repository to update and fix the bug in our code. In this short guide, we will learn to add a remote branch to a repository.

Git Remote

The command git remote generates, watches, and removes connections to further repositories.

Add a Remote Repository in Git

If we want to add a new remote, we will run the command git remote add on the terminal, in which the directory of our repository is stored, and this command will add a new branch to our repository.

The command git remote add is based on two arguments.

  1. The first part is a remote name in a command.
  2. The last part is a remote URL in a command.

For example:

$ git remote add <newname> <url>
# Set a new remote

List Remote Branches in Git

Once the remote branch is created successfully, we can list all the remote branches through the command git branch attached with the alias _r and check whether the newly created remote branch is in the list.

git branch -r

Create a Local Branch in Git

First, we’ll create a local branch with the help of the git checkout command.

git checkout -b <new-branch-name>

With the help of this command, we created a new branch from our current branch. If we want to develop a new branch from another branch, we will specify the branch name that we want at the last of the command, as mentioned below in the example.

git checkout -b <new-branch-name> <from-branch-name>

Push the Local Branch to Remote in Git

We mostly work on a local branch, and whenever we are ready to share with our code mates in a team environment, we push it to the remote repository by executing the following command.

git push -u <remote> <branch-name>

The alias -u is used as the shortcut for --set-upstream. This will help us set up the remote branch for the current local branch.

After that, whenever our code mates need to interact with our branch, they will run the git fetch command.

git fetch
git checkout <branch-name>

Merge Remote Branch in Git

Now we will merge the changes from remote with local through the following command.

git merge <remote>

Update Remote in Git

Afterward, we will update the remote from the local branch with the following command shown in the example.

git push -u <remote> <branch>
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Remote