Git

How Do You Push Just a Single Git Branch (and no Other Branches)?

Developers make changes in the project files and update the local repository by committing along with the specific statement known as a commit message for saving purposes. Then, they are required to push these changes to the centralized server. Additionally, users can push the changes from a specific local branch to the remote branch without adding the content of all branches.

This guide provided the method of pushing a single Git branch to the GitHub server.

How to Push Only One Git Branch (and no Other Branches)?

In order to push a single Git branch to the GitHub server, follow the provided instructions:

  • Redirect to the required Git directory.
  • Check the list of all branches.
  • Choose the target branch and switch to it.
  • Ensure the remote URL list.
  • Pull the latest version of the specific remote branch.
  • Execute the β€œgit push –force <remote-name> <target-branch-name>” command.

Step 1: Switch to Git Repository
Redirect to the particular Git repository by executing the β€œcd” command along with its path:

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

Step 2: Check Local and Fetched Remote Branches
Then, run the β€œgit branch” command to display the fetched remote and existing local branches

$ git branch -a

Here, the β€œ-a” option represents the all. Now, all the existing branches are displayed. We have selected the below-highlighted β€œalpha” branch, which needs to push to the GitHub server:

Step 3: Switch to Target Branch
Next, execute the β€œgit checkout” command and navigate to the previously selected local branch:

$ git checkout alpha

Step 4: Verify Backend Connection Between Local and Remote Repositories
To ensure the GitHub server and local machine are connected with each other, use the provided command:

$ git remote -v

Step 5: Pull Remote Branch
After that, execute the β€œgit pull” command along with the remote name and specify the branch to pull the content of the particular branch:

$ git pull origin alpha

Step 6: Push Single Branch
Finally, utilize the β€œgit push” command for updating the GitHub server with the local changes:

$ git push --force origin alpha

Here:

  • β€œ–force” option is used for pushing the local branch content forcefully.
  • β€œorigin” is the name of the remote URL.
  • β€œalpha” is a particular branch that needs to be pushed.

As you can see, we have successfully pushed the content of the single file to the GitHub remote repository:

That’s it! We have explained the easiest way of pushing a single Git branch to the GitHub server.

Conclusion

To push a single Git branch to the GitHub server, first, redirect to the required Git directory. Then, check the list of all branches and select one of them. Next, switch to it and check the remote URL list. After that, pull the updated content of the specified remote branch. Lastly, execute the β€œgit push –force <remote-name> <target-branch-name>” command. This guide demonstrated the procedure of pushing a single Git branch to the GitHub server.

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.