This write-up will illustrate the method to add the local branch to the remote Git repository.
How do Add a Local Branch to the Remote Git Repository?
To add the local branch to the remote Git repository, follow the below-listed instructions:
- Redirect to the desired local repository.
- Choose the particular local branch.
- Add a remote URL and verify it.
- Add the local branch to the remote Git repository through the βgit push –set-upstream <remote-name> <branch-name>β command.
- Ensure changes on the GitHub repository.
Step 1: Navigate to Local Repository
First, enter the following command and redirect to the particular local repository:
Step 2: View Available Branches
Then, list all the available branches:
The given output indicates that the current repository contains two local branches and βmasterβ is the current working branch.
Choose the desired branch that you want to add to the GitHub repository. For instance, we have selected the βmasterβ branch:
Step 3: View Branch Content
Next, display the content of the current working branch through the provided command:
You can view the content of the βmasterβ branch:
Step 4: Add New Remote
After that, add the new remote in the local repository to connect it with the remote repository:
Step 5: Verify Remote URL
Next, type out the below-listed command to ensure that the remote has been added to the local repository:
Step 6: Add Local Branch to Remote Repository
Now, run the βgit pushβ command to push the local branch to the GitHub repo:
Here:
- β–set-upstreamβ is used to set the tracking information while pushing local content to the remote repository.
- βoriginβ is the remote name.
- βmasterβ is the name of the remote branch:
Step 7: Verification
Lastly, verify the changes on the GitHub repository:
It can be seen that the content of the local βmasterβ branch has been added to the GitHub repository:
Conclusion
To add the local branch to the remote Git repository, first, switch to the local repository and choose the local branch that needs to be added to the GitHub repository. Next, add the remote URL to link the local directory with the remote repository. Finally, execute the βgit push –set-upstream <remote-name> <branch-name>β command and verify changes on GitHub. This write-up illustrated the method to add the local branch to the remote Git repository.