Git push origin uploads your committed local changes to the remote repository named origin, updating the corresponding branch on that remote. You run it after committing your work locally, and it sends those commits to the remote so others can see and pull them.
What Exactly Is origin?
When you clone a repository, Git automatically creates a remote called origin that points to the URL you cloned from. It is simply a default name—you can rename it, delete it, or add other remotes with different names. The command git remote -v shows you the URLs associated with each remote, letting you confirm what origin refers to in your project.
If you started a project locally and want to push it to a new remote, you must first add that remote explicitly:
git remote add origin https://github.com/user/repo.git
After that, origin exists in your configuration, and git push origin will work as expected.
How git push origin Works
The full command is git push origin <branch-name>. If you omit the branch name, Git uses the current branch’s upstream tracking configuration. For example, git push origin main pushes your local main branch to the main branch on the origin remote. The command updates the remote branch only if your local commits are ahead of it; if the remote has commits you don’t have locally, the push is rejected to prevent overwriting others’ work.
Behind the scenes, Git packs the objects (commits, trees, blobs) and sends them over the network using the smart HTTP or SSH protocol. The remote then updates its refs to point to the new commit, and the push is complete.
Common Use Cases and Variations
- First push of a new branch:
git push -u origin feature-branchpushes the branch and sets upstream tracking, so future pushes can be justgit push. - Push all local branches:
git push origin --allpushes every local branch to the remote, creating or updating them as needed. - Push tags:
git push origin --tagssends all local tags to the remote, which is useful for marking releases. - Force push (careful!):
git push origin branch --forceoverwrites the remote branch with your local state. Use only when you are certain, as it can destroy others’ commits.
Each variation serves a different purpose, but the core idea remains the same: sending local commits to a specific remote.
Common Problems and How to Fix Them
Non-fast-forward error
If the remote has commits you don’t have, Git rejects the push with a message like “! [rejected] main -> main (fetch first)”. The fix is to pull the remote changes first (git pull origin main), resolve any merge conflicts, then push again. Avoid force-pushing in this situation unless you intentionally want to discard the remote commits.
Authentication failures
When using HTTPS, Git may prompt for a username and password. If you have two-factor authentication enabled, you need a personal access token instead of your password. For SSH, ensure your SSH key is added to your account and that you are using the correct SSH URL.
“fatal: ‘origin’ does not appear to be a git repository”
This error means the remote origin is not configured. Check with git remote -v and add the correct URL if missing.
Choosing Between git push and git push origin
If your branch has an upstream set, git push is equivalent to git push origin <current-branch>. However, if you work with multiple remotes (e.g., a fork and the original repository), specifying the remote name is essential. git push origin explicitly targets the origin remote, while git push uses the default remote (usually origin) but only for the current branch. For clarity and to avoid surprises, many developers always include the remote name.
Safety and Best Practices
- Always pull before pushing when collaborating on a shared branch to minimize conflicts.
- Never force-push to shared branches unless you have coordinated with your team, as it can erase their work.
- Use branches for features and push them regularly to back up your work and enable collaboration.
- Review your commits with
git logbefore pushing to avoid sending unintended changes. - Use
git push --dry-runto see what would be pushed without actually sending anything.
These practices keep your remote history clean and prevent data loss.
Frequently Asked Questions
What is the difference between git push and git push origin?
git push uses the upstream tracking configuration of the current branch to determine the remote and branch. git push origin explicitly specifies the remote as origin and, if no branch is given, pushes the current branch to its matching branch on origin. For most workflows, they are equivalent, but being explicit avoids ambiguity when multiple remotes exist.
Why does git push ask for a password every time?
When using HTTPS, Git may ask for credentials on each push. You can avoid this by using a credential helper (e.g., git config --global credential.helper cache) or by switching to SSH authentication, which uses keys instead of passwords. For GitHub and GitLab, you must use a personal access token instead of your account password.
How do I undo a push?
If you pushed a commit that you want to remove, you can git reset --hard HEAD~1 locally to remove the last commit, then force-push with git push origin branch --force. However, this rewrites history and can affect others who have already pulled. A safer approach is to revert the commit with git revert, which creates a new commit that undoes the changes, and push that normally.
What does “upstream” mean in git push -u origin main?
Setting the upstream with -u records the relationship between your local branch and the remote branch. After that, you can simply run git push or git pull without specifying the remote and branch, because Git knows where to send and fetch changes.
Can I push to a remote that is not named origin?
Yes. You can add a remote with any name, such as git remote add upstream https://github.com/other/repo.git, and then push to it with git push upstream branch. This is common in fork workflows where origin points to your fork and upstream points to the original repository.