Git

How to Git Push to Remote Branch

Git is a decentralized versioning control system that contains local and remote branches. In Git, branches are utilized to develop multiple modules independently from the main workflow. When developers work locally, they commit to their local branch and then push it to the remote branch to update the project source code.

In this blog, we will explain the method to Git push to the remote branch.

How to Git Push to Remote Branch?

To Git push to the remote branch, first, navigate to the Git local repository and display the list of all local branches. Then, execute the “$ git switch <branch-name>” command to switch to the branch. Next, list the content of the local repository and run the “$ git push -u origin <branch-name>” command to push to the remote branch.

Step 1: Move to Git Local Repository

At first, navigate to the Git local directory by executing the “cd” command:

$ cd "C:\Users\nazma\Git\Demo12"

 

Step 2: List Git Local Branches

Next, run the “git branch” command to view all local branches:

$ git branch

 
According to below-given output, our local repository contains multiple branches. We will select one of them. For instance, “master”:


Step 3: Switch to Branch

Now, switch to the master branch by utilizing the provided command:

$ git switch master

 

Step 4: List Repository Content

To view the list of content of the repository, run the “ls” command:

$ ls

 
As it can be seen that the remote repository named “demo” is already cloned:


Step 5: Push Remote Branch

Finally, execute the “git push” command with the option “-u” to push the “master” branch to the remote repository:

$ git push -u origin master

 
The below output shows that the “master” is pushed successfully:


Note: In the above command, we have used the “-u” option. This option is used when performing the first push of any branch only. This creates a tracking branch for the push branch. Next time you push anything to the already pushed branch, there is no need to push it without the “-u” option.

Conclusion

To Git push to the remote branch, first, move to the Git local directory and view the list of existing local branches by executing the “$ git branch” command. Next, run the “$ git switch <branch-name>” command to switch the branch to which you want to push to the remote branch. After that, list the content of the local repository and run the “$ git push -u origin <branch-name>” command to push to the remote branch. This blog demonstrated how to Git push to the remote branch.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.