The git push origin
command transfers commits from your local repository to a remote repository. This command updates the remote branch with commits from your current branch. This guide will cover how to use git push origin
and its variations to manage your code in a remote repository effectively.
Basic usage of git push origin
What is origin?
In Git, "origin" is the default name given to the remote repository from which your local repository was cloned. You can push your changes to this remote repository using the git push origin
command.
Git push origin command
To push the current branch to the corresponding remote branch run:
git push origin
This command pushes changes from the local branch you are currently on to the remote branch with the same name.
Pushing to a specific branch
Git push origin branch
If you want to push changes to a specific branch, you can specify the branch name:
git push origin <branch-name>
For example, to push to a branch named feature
:
git push origin feature
Git push origin main
If you are working with the main branch and ready to update the remote main branch, use:
git push origin main
This command specifically targets the main branch, ensuring that your local main branch's changes are pushed to the remote main branch.
Setting upstream tracking with git push
Git push -u origin
The -u
flag sets the upstream tracking information for the branch. This means that once you set the upstream branch with this command, you can use git push
and git pull
without specifying the branch in future commands.
git push -u origin <branch-name>
For example, to set upstream tracking for a branch named feature
, run:
git push -u origin feature
Git push -u origin main
To set the upstream tracking for the main branch, ensuring that future pushes and pulls target this branch by default, use:
git push -u origin main
Practical example
Let's consider you have a local branch named feature
that you've been working on and are now ready to push to the remote repository. Here's how you would push these local changes to the remote:
Check the current branch:
Terminalgit branchThis command will show you which branch you are currently on.
Switch to the feature branch (if not already on it):
Terminalgit checkout featurePush the feature branch to origin with upstream tracking:
Terminalgit push -u origin featureThis configures the feature branch to track the remote feature branch, simplifying future push and pull commands.
Verify that the push was successful:
Terminalgit statusThis will confirm that everything is up-to-date and has been pushed correctly.
For further reading see the official Git documentation.