How to Push Local Branch to the Remote Branch in Git
Git is a platform that uses branches to develop features from the main workflow. It is a deconcentrated versioning system because we have both local and remote branches on our repository.
When we work independently, we commit to our local branch, but what do we have to do if we want to share our commits with our colleagues? For this case, we will push
our Git branch to the remote repository.
This article will show how to push a Git branch remotely for this action.
The git push
command is used here to transfer local repository data to a remote repository.
Push
is a method to move commits from our local repository to the remote repository. However, we can say it’s equivalent to git fetch
, as git fetch
is used to import commits to our local branch while the git push
command exports commit to our remote branch.
The git remote
command here is used to make remote branches. The push command can overwrite changes, while we should be cautious when pushing our changes to the remote branch.
The concerns are mentioned below:
- The
push
command is used to transfer new local commits data to a remote repository. - The branch from which we are uploading the data should always be present at the checked-out
HEAD
branch in our local environment. - The branch to which we are uploading data should be mentioned clearly in the command’s options. However, these options can be skipped if a tracking connection with a remote branch is already set up.
Push Local Branch to Remote Branch
Furthermore, the push
command is also used to delete a remote branch in some cases.
Before running the git push
command, the correct local branch should be checked out first. After that, for performing the push
command, highlight which remote branch you want to push to:
git checkout develop
git push origin develop
Push Local Branch for First Time
Here we can use the option -u
if we create a local branch for the first time on a remote repository; this would be very helpful for the future. It guarantees that a tracking relationship is built between a local and a remote branch that we have recently made.
git push -u origin develop
After setting up a tracking relationship, we can execute further pushes without explicitly providing additional changes since the tracking relationship supplies default values for the push command. We use the ‘–delete ’ option for deleting a remote branch and state which branch we want to delete in the parameter.
The following is the command to delete the branch from the remote repository:
git push origin --delete feature/login
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn