git push origin is a git command that pushes a local branch(es) to a remote repository (origin).
The general format for the git push command is:
$ git push [remote repository] [flags] [local repository]
Run in Warp
origin is the conventional shorthand name of the url for the remote repository (usually in GitHub or another cloud git repository provider) for your project.
Choosing which branches to git push origin
You can choose which branch(es) to push to origin:
git push origin <branchname> will push the current branch to the remote counterpart of that branch.
git push origin will push the current branch to the branch of the matching name in the remote repository (aka, “branch configured upstream”), if it exists, otherwise, it will not push and notify that the current branch has no remote counterpart (error message: “<branchname> has no upstream branch”).
The default branch in your project is conventionally a branch named “main”. This branch is the version of the project that goes into production or the version from which you will create further branches to isolate changes, and merge back into the default branch.
If a project you are working on is older, the default branch might be named “master”, which GitHub changed to remove references to slavery in conventional terminology. It’s important to check the name of the default branch.
Pushing to the default branch can be done using:
$ git push origin main
Run in Warp
Or in the case of an older repository:
$ git push origin master
Run in Warp
Where git push initiates the push, origin refers to the remote counterpart of the project, and main is the branch name. This is common when you are the only contributor to your project, and you want to directly edit the default branch of your project with changes.
When should I use git push origin vs git push?
NOTE: The behavior described is for git versions 1.7.11 or higher.
git push and git push origin will both push the current branch to the remote counterpart. If the original git configurations are being used, git push assumes the current branch is the one to push, and assumes that remote is origin.
However, there are some cases where you might want to explicitly use git push origin:
- More than one remote repository exists such that the remote repository to be pushed to must be specified
- You want to push a local branch to a remote branch of a different name
- You are adding additional flags, such as in git push origin --delete, and want to be explicit
git push origin flags
git push origin <flag>:
- -u, or --set-upstream:
- This creates a remote branch and sets it upstream of the current branch you are pushing. The relationship between the current branch and upstream branch is remembered, such that you will not have to continually connect the remote and local branches when pushing commits.
- -f, --force:
- Pushes that would delete or overwrite existing code are usually blocked. With this command, pushes from your local repository would be forced onto the remote repository, potentially deleting or overwriting other commits!
- -d, --delete:
- Deletes the remote branches listed:
- git push origin --delete <branch name>
- --all:
- Pushes all local branches to remote repository
For a comprehensive list of flags, visit the git push documentation.
TIP: If you’re sure you’re on the branch you want to push, you can use symbolic references like HEAD to grab the <branchname> of the current branch without having to type it out. The git push origin HEAD command will push the current branch to the remote counterpart on github or some other server, if it exists.
How to solve the no upstream branch error message
Use:
$ git push --set-upstream origin
Run in Warp
To create a remote branch with the same name as the local branch and push changes to the remote branch (aka, “set upstream”).
Help! git push origin still isn’t working!
Try these troubleshooting tips:
- Check that you are on the branch you want to push using git status
- If using remote and local repository names or references explicitly, make sure they are spelled correctly
Written by
Amanda Khoo
PhD, MSc in Data Science
Filed Under
Related Articles
Undo A Git Pull
How to effectively remove the commits introduced by a pull in Git using git-reset and preserve your local changes using git-stash. Also, how to cancel an unmerged pull request on GitHub.
Undo a Git Merge
How to rollback the changes introduced by a merge in Git by adding new opposite commits using git-revert and effectively removing commits using git-reset.
Prompt Show Git Branch In Prompt
Enhance your terminal with a custom Git prompt. Learn different ways to integrate this contextual info, from custom shell functions to Warp context chips and toolkits like Starship and P10K.
How To Remove Secrets From The Git History Remove Secrets From The Git History
Learn how to remove secrets from the Git history using the BFG and git-filter-repo command-line tools.
Adding a Submodule in Git
This post will show you how to simply add a submodule to a local repository, clone a repository with a submodule, and work within a repository that has a submodule.
Undo a git push
This post will show you had to simply undo a git push three different ways.
Undo Git Add
Learn how to effectively use 'git add' to stage files in Git for committing, and discover two powerful methods to undo accidental stagings.
Undo a Git Rebase
This post will show you how to undo a rebase using git reset, git rebase and git revert
Create Folder In GitHub Repository
Learn how to create and push one or more empty directories in a Git repository using `.placeholder` and `README.md` files using both the CLI and the GitHub interface.
Git Push Tags
This post will show you how to push a single tag, multiple tags, all tags, and tags with commits.
Undoing Git Commits
Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.
Delete Local Git Branch
Learn how to delete local branches from your git repository, including ones with unmerged changes, as well as local remote-tracking branches.