Push and track new local Git branches to the remote repository
This article will discuss how we can push and track new Git branches to a remote repository. Developers often have to create new branches when working on a project and publish them to a remote repository for other developers to access the changes.
But before we get into the details, let's check out some useful commands when working with Git branches.
Commands for working with branches in Git
Here are some handy commands that you should have at your fingertips when using Git.
List branches
We run the following command to view the branches in our Git repository.
git branch
For both our local and remote repos we run this command.
git branch -a
If you are only interested in the remote repository, run this command.
git branch -r
example:
$ git branch -a
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
The asterisk ( *
) points to the branch we are currently working on.
Creating a new branch in Git
We use git branch
the command while mentioning the name of the branch we want.
git branch <branch-name>
Git will forward the commits from the parent branch to the new branch. The parent branch is the branch we were working on when we created the new branch.
The above command will only create the specified branch. To switch to a new branch, use the command in the context shown below git checkout
.
git checkout <branch-name>
To make it easier, run the following command.
git checkout -b <branch-name>
This command will create and switch to the mentioned branch-name
.
Renaming a branch
To rename a branch in Git, we run the following command.
git branch -m <old-branch-name> <new-branch-name>
Push local branches to remote repositories in Git
We run the command while mentioning the branch name git push
. See the command below.
git push -u origin <branch-name>
Git will push the branch and set up tracking. -u
is --set-upstream
the abbreviation for .
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.
Related Articles
Git shows remote tracking branches
Publish Date:2025/04/03 Views:125 Category:Git
-
Branches on remote Git repositories are called remote branches. These are pointers to our remote repositories, including branches, tags, etc. Local branches only exist on each developer's local personal computer, but there is only one remot
View merged and unmerged branches in Git
Publish Date:2025/04/03 Views:96 Category:Git
-
This article discusses how to list merged and unmerged branches in Git. Git branches encourage convergent evolution of code. This is where we create a branch as a temporary space to work on a feature, and then merge the branch with its orig
The difference between Fork and Branch on GitHub
Publish Date:2025/04/03 Views:157 Category:Git
-
This article discusses the difference between Form and Branch on GitHub. In the context of coding, the current era relies more on collaboration. GitHub is one of the most commonly used collaboration tools. Forking and branching on GitHub ar
How to determine the current branch in Git
Publish Date:2025/04/03 Views:164 Category:Git
-
Git is a unique and popular version control system that is used by most of the software developers to keep an eye on the changes made in various applications and stay connected with other teams on the running projects. It helps large teams
Difference between Git Merge Origin/Master and Git Pull
Publish Date:2025/04/03 Views:195 Category:Git
-
This article outlines the differences between the git merge origin/master and git pull commands. These two commands integrate changes from a remote repository into the current local branch. However, each command's operation is unique and ha
Git Pull Origin branch overwrites Master branch
Publish Date:2025/04/03 Views:143 Category:Git
-
This article explains how we can revert the changes made to the master git pull origin branch branch after running the command . Assume that you have a master branch and a feature branch in your local and remote repositories . You pull chan
Git Pull and Merge Commands
Publish Date:2025/04/03 Views:56 Category:Git
-
In the vast world of version control systems, Git is the only fast, well-organized, in-demand, and easily accessible version control system that enables users to work on various projects simultaneously in an efficient manner without any col
Difference between Git Checkout --Track Origin/Branch and Git Checkout -B Branch
Publish Date:2025/04/03 Views:185 Category:Git
-
This article outlines the difference between the git checkout -b branch origin/branch and git checkout --track origin/branch commands. We use these two commands to check out remote branches in a Git repository. These two commands have the s
Rolling back to an old commit in a public Git repository
Publish Date:2025/04/03 Views:115 Category:Git
-
This article explains how we can roll back a public Git repository to an old commit. When using Git version control, we can go back to any desired point. Without further ado, let’s get into today’s agenda. Rolling back to an old commit