How to Git Push to Remote Branch

March 30, 2023

Introduction

Branches in Git are independent development lines in a project. When working on a local copy of the repository, you can create new branches in the local repository that don't yet exist in the remote one until you push the changes.

In this tutorial, you will learn how to push a branch to a remote repository in Git.

How to Git push to remote branch - a tutorial.

Prerequisites

How Do I Push a Branch to Remote in Git?

Pushing a branch to a remote repository allows developers to share their work with other collaborators on the project. That way, other developers can implement features or bug fixes into their own work or review your changes.

The sections below show different options for pushing a branch to a remote repository.

Push the Main Branch to Remote

The main (or master) branch is the default branch that Git automatically creates when you initialize a repository. If you have created a repository locally and need to push the main branch to a remote, you are likely pushing changes for the first time.

Follow the steps below to push the main branch to remote and ensure the files are in the tracking index and that they have been committed.

Step 1: Check if Git is tracking all the necessary files.

Run the following command to check if Git is tracking all the files in the repository:

git status
Check the git status of a repository.

The output shows if there are any files in the repository that Git is not tracking. If there are, add all the files to the tracking index by running:

git add .

Step 2: Commit any changes to the local branch.

Make sure that all the changes from the local repository have been committed. Use the following syntax to create a commit:

git commit -m "<commit message>"
Creating a new commit in Git.

Step 3: Fetch the changes from the remote repository.

If you have already set up a remote repository and someone else is working on the project, make sure to fetch and merge the latest changes.

git pull

If you haven't already set up a remote repository, skip this step.

When there is no difference between the remote and local repository, the output states so. If there are any changes, Git merges them. However, it is possible that there will be merge conflicts, so resolve the conflicts first to complete the merge.

Step 4: Add the remote server.

If you haven't already set up the remote server, add it to your repository using the following syntax:

git remote add origin <remote_url>

For example:

Adding the new origin repository.

Step 5: Switch to the master branch.

Make sure to switch to the branch you want to push, in this case the main/master branch. Use git switch or git checkout:

git checkout master

Note: The master and main branches are two names for the same default Git branch. Check which name you are using by running git branch.

Step 6: Push the branch.

After switching to the master branch, push it to the remote using the following syntax:

git push -u <remote_name> <branch_name>

The -u flag adds the upstream (tracking) reference for every branch you successfully push.

For example:

git push -u origin master
Pushing the master branch to the remote repository.

If this is the first time you are pushing the branch to the remote repository, Git creates the branch and adds all the changes. If the branch already exists, Git updates it.

Push a Branch with a Different Name to Remote

Git allows you to push a local branch to a remote one with a different name. Follow the steps below:

Step 1: Pull changes.

Ensure that your local repository has all the changes contained in the remote one. Run the following command:

git pull
Pulling changes from the remote repository.

If everything is up to date, the output says so. Otherwise, the changes are downloaded and merged into your repository.

Step 2: Switch to the target branch.

Switch to the branch you want to push to the remote repository. The syntax is:

git switch <branch_name>

For example:

Switching to a new branch in Git.

Step 3: Push the branch to remote.

Use the git push command to push a local branch to a remote one with a different name. Specify the local branch name and the remote name separated with a colon. The syntax is:

git push <remote_repository> <local_branch>:<remote_branch>

For example:

git push origin new-feature:feature
Pushing a branch with different name to remote repository.

The command pushes the new-feature branch to the feature branch in the remote origin repository.

Push Changes to Another Branch

Git allows users to push the changes to another branch on the remote repository by specifying the remote name, the local branch name, and the remote branch name.

Follow the steps in the sections below.

Step 1. Pull changes from the remote.

Before pushing changes to the remote repository, perform a pull to fetch any changes from the remote branch and integrate them into your current local branch:

git pull

Step 2. Switch to the branch you want to push.

Use git switch or git checkout:

git switch <branch_name>
Switching branches in Git.

Step 3. Merge with the remote branch.

Merge the local branch with the remote upstream branch. For the merge to succeed, the tip of the remote branch cannot be behind the branch you want to push. The syntax is:

git merge <remote_name>/<remote_branch>

For example, to merge with the feature branch in the origin remote repository, run:

git merge origin/feature

Resolve any merge conflicts that may arise.

Step 4. Push the changes.

Use the following syntax to push the changes to the specified branch:

git push <remote_name> <local_branch>:<remote_branch>

For example, to push the my-feature branch to the feature remote branch, run:

git push origin my-feature:feature
Pushing Git changes to another branch.

Push Branch to Custom Repository

The git push command also allows you to push a branch to a custom remote branch, not only the one defined as origin. Follow the steps below to add a new remote and push your changes to that custom repository.

Step 1. Add a new remote.

Check which remotes are defined in your repository by running:

git remote -v
Listing remotes in a Git repository.

To add a custom remote, use the git remote add command and specify a unique repository name and URL.

The syntax is:

git remote add <remote_name> <remote_URL>

For example:

git remote add custom https://github.com/bosko-pnap/custom.git
Adding a new remote to a Git repository.

Running git remote -v again shows that the new remote has been added to the list.

Step 2. Push the changes to custom repository.

To publish your changes to a custom remote, use the git push command and specify the correct remote name and which branch you want to push.

The syntax is:

git push <remote_name> <branch_name>

For example:

git push custom my-feature
Pushing changes in Git to a custom remote.

The command pushes the new-feature branch to the custom repository.

Note: Learn how to apply a single commit from one branch to another using Git cherry-pick.

Conclusion

This tutorial showed several different options for pushing a local branch to a remote repository. Pushing changes in a remote repository is useful when collaborating on a project. For that reason, git push is one of the essential Git commands.

For more important Git commands, download our Git commands cheat sheet, or get started using Git with our Git beginner's guide.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Git Merge Master into Branch
March 22, 2023

This tutorial shows two methods for merging the master branch into another branch in Git. See examples and use cases.
Read more
How to Pull All Branches in Git
March 16, 2023

This tutorial shows how to pull all branches from a remote repository into a local one in Git. See examples and download an entire repository in a few steps.
Read more
Git Rebase vs. Git Merge: What's the Difference?
March 15, 2023

This article compares git rebase and git merge, commands that allow users to implement changes into the main branch.
Read more
SSH vs. HTTPS for Git: Which One Should You Use?
February 21, 2023

This article compares SSH and HTTPS for Git - two network protocols used to secure the connection to remote repositories.
Read more