How to Push Git Branch to Remote
Branches are a great way of working on something new as it provides developers an independent workspace where they can try out things without worrying about affecting the rest of the project. When working with other collaborators, we will have to share our code with them. This is done by pushing our local branches to a central remote repository. Other developers can then pull these branches and view the changes made by us. Let's learn how to push branches to a remote repository.
Git Push command is used to push our local changes to the remote repository. Remember to first pull the changes from the remote repository before pushing. This updates our local branch with any new changes that were made to the remote branch by some other developer. If the commits of the remote branch are not present on our local branch then Git will block the push. We can force a push in such situations by using the -f or --force options but it is not recommended as the changes of the remote branch will be lost.
The following image illustrates the above scenario. We can see that Git rejects the push and suggests that we first pull the changes from the remote repository.
We also need to have a remote connection with the repository where we are trying to push. Use the Git Remote Add command to add a new remote.
$ git remote add <remote-name> <remote-repo-url>
We are ready to use the Git Push command. Let's look at the different scenarios and learn how to push local branches in these cases.
Pushing a Local Branch
Consider the case where we have a remote branch called feature and we have a copy of that branch in our local repository with the same name(feature). We have added a few commits to this branch and now we want to push it. To do this, we need to run the Git Push command and enter the remote name and the name of the branch we are trying to push(feature, in this case).
$ git push <remote> <branch-name>
The follows image shows the output of the above command.
Pushing to a Branch with Different Name
There may be cases where the name of our local branch is different from the name of the remote branch where we want to push. In this case, we can use :(colon) to separate the local branch name from the remote branch name. Note that Git will only push if all the changes of the remote branch are present in the local branch.
$ git push <remote> <local-branch-name>:<remote-branch-name>
Setting Upstream Branches
It is a bit tedious to enter the remote name and the branch name every time, we want to push to the remote repository. A solution to this problem is to set upstream branches for our local branches. By doing this Git will automatically know the remote and the branch to which we are trying to push.
All we have to do is switch to the branch that we want to push and run a simple Git Push command. Another advantage of upstream branches is that we can easily compare our remote branch and local branch by using the Git Status command.
We can set up an upstream connection the first time, we are pushing a branch by using the -u or the --set-upstream command.
$ git push --set-upstream <remote> <branch-name>
Now, we no longer need to use the remote name and branch name in the future when trying to push this branch.
We will often Push our local branches to a remote repository when working in a team and collaborating with other developers. Git Push command is used to push branches to the remote repository. One thing to keep in mind before pushing is that our local branch should have all the commits that were made to the remote branch. If this is not the case then Git will block a push. We can avoid this by pulling before pushing. It is also a good idea to set upstream branches as they make it very easy to push and pull and also helps us to compare remote and local branches.