Branches are the the main elements in Git that help developing independent features on the same repository.
Normally, we often work locally and only commit to the local branch. But if you’re working with a team, you would want to share those commits to the teammates who are also contributing to the same branch.
In this article, we would show you how to push commits into a specific Git branch instead of the main
/master
one.
Push local branch to specific branch with same name
Suppose your local branch and remote branch are using the same name, you can simply run the following command to push the current branch named devbranch
to a remote branch with the same name.
git push origin devbranch
If you want to set the upstream linking the local branch to the remote branch, so that the next time you can simply do git push
, run the command with -u
option :
git push -u origin devbranch
The -u
option in the command stands for --set-upstream
, which tells Git to track the devbranch
in the remote repository.
Any future git push
will only push the current branch, and only if that branch has an upstream branch with the same name.
Push local branch to specific branch with different name
In case the local and remote branch name is different, run the following command to push localBranchName
to the remoteBranchName
.
git push origin localBranchName:remoteBranchName
Code language: CSS (css)
You can also add -u
option to enable upstream tracking, so that the next time you can simply do a git push
without specifying a destination.
git push -u origin localBranchName:remoteBranchName
Code language: CSS (css)
We hope that the short article provide helpful information on which command to use when you want to push to specific branch in Git and improve your programming workflow. You may want to check out our other guides on accept all current/incoming changes in Git, easily handling merge conflicts in VSCode and a basic tutorial on how to clone a private Github repo.
If you have any question, don’t hesitate to let us know in the comments section below.