JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Adding a remote branch in Git

Author:JIYIK Last Updated:2025/03/30 Views:

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

In every version control system, branches are considered as the best way of code management and help us to create remote branches in Git. Branches are used every day in development operations to separate 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 need to add a new project or feature in our code development, or we want to fix a bug that has been noticed by a client or a quality assurance person in our team, we will generate a new branch repository in the same branch to update and fix the bug in our code. In this short guide, we will learn how to add a remote branch to a repository.


Git Remote

Commands git remoteGenerate, monitor, and delete connections to additional repositories.


Adding a remote repository in Git

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

The command git remote addis based on two parameters.

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

For example:

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

Listing remote branches in Git

After the remote branch is created successfully, we can list all remote branches through _rthe command with alias git branchand check whether there is a newly created remote branch in the list.

$ git branch -r

Creating a local branch in Git

First, we will git checkoutcreate a local branch with the help of the command.

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

With the help of this command, we have created a new branch from the current branch. If we wanted to develop a new branch from another branch, we would specify the branch name we want at the end of the command, as shown in the example below.

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

Pushing local branches to remote in Git

We mainly work on local branches 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 -uis used as --set-upstreama shortcut for . This will help us set the remote branch for the current local branch.

Afterwards, whenever our code partners need to interact with our branch, they will run git fetchthe command.

$ git fetch
$ git checkout <branch-name>

Merge remote branches in Git

Now we will merge the changes from remote and local using the following command.

$ git merge <remote>

Updating remotes in Git

After that, we will update the remote from the local branch using the following command as shown in the example.

$ git push -u <remote> <branch>

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing drives in Git Bash

Publish Date:2025/03/30 Views:56 Category:Git

This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to

Creating and using branches in Git

Publish Date:2025/03/30 Views:99 Category:Git

This article introduces Git branches. We will see how Git branches can help you organize your projects. Some of the commands we will deal with are git branch and git checkout . git branch Use commands to create, display, and delete branches

Git force pull

Publish Date:2025/03/29 Views:166 Category:Git

In this tutorial, we will learn how to force pull changes from a remote repository in Git. Sometimes, we may need to discard local modifications and replace them with updates from a remote repository in a collaborative development environme

Git pulls Master into the branch

Publish Date:2025/03/29 Views:193 Category:Git

When developing software using the Git tool, you can create different branches for different features. When you make changes to master, these changes are not automatically added to other branches. This article will explain how to pull all t

Installing Git in Cygwin

Publish Date:2025/03/29 Views:116 Category:Git

Git is considered an active, innovative and highly recommended distributed version control system with a fantastic standalone command line while providing us with advanced features and complete internal methods. What is Cygwin Cygwin is con

Displaying remote repository information in Git

Publish Date:2025/03/29 Views:140 Category:Git

This tutorial is about displaying information about remote repositories in Git. We use Git, a version control system, to track changes made to files in our project directories through Git repositories. Usually, local repositories are tracke

Deleting a remote repository in Git

Publish Date:2025/03/29 Views:131 Category:Git

When we commit the wrong data to the origin, push it to the origin and merge it to the current branch. But later we realize that we don't need to do the merge in that repo, so the question here is how to undo or revert the merge commit that

Setting up a Git remote repository

Publish Date:2025/03/29 Views:78 Category:Git

This article will explain how to add or delete remote repositories. A remote repository is a project hosted somewhere, such as Github/Gitlab. Adding a remote repository allows us to use a short name (alias) to execute commands instead of ty

.git Directory Explanation

Publish Date:2025/03/29 Views:66 Category:Git

In this article, we'll introduce Git folders .git . We'll cover why Git creates folders and what they contain. .git What are folders in Git ? Git is a widely used version control system. Git repositories store the changes you make in your p

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial