Git Push Origin

Last Updated 2022-12-15

[.inline-code]git push origin[.inline-code] is a git command that pushes a local branch(es) to a remote repository ([.inline-code]origin[.inline-code]).

The general format for the git push command is:

 $ git push [remote repository] [flags] [local repository]

[.inline-code]origin[.inline-code] 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-branches]Choosing which branches to [.inline-code]git push origin[.inline-code][#choosing-branches]

You can choose which branch(es) to push to origin:

[.inline-code]git push origin <branchname>[.inline-code] will push the current branch to the remote counterpart of that branch.

[.inline-code]git push origin[.inline-code] 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

Or in the case of an older repository:

 $ git push origin master

Where [.inline-code]git push[.inline-code] initiates the push, [.inline-code]origin[.inline-code] refers to the remote counterpart of the project, and [.inline-code]main[.inline-code] 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.

[#origin-vs-push]When should I use [.inline-code]git push origin[.inline-code] vs [.inline-code]git push[.inline-code]?[#origin-vs-push]

NOTE: The behavior described is for git versions 1.7.11 or higher.

[.inline-code]git push[.inline-code] and [.inline-code]git push origin[.inline-code] will both push the current branch to the remote counterpart. If the original git configurations are being used, [.inline-code]git push[.inline-code] assumes the current branch is the one to push, and assumes that [.inline-code]remote[.inline-code] is [.inline-code]origin[.inline-code].

However, there are some cases where you might want to explicitly use [.inline-code]git push origin[.inline-code]:

  • 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 [.inline-code]git push origin --delete[.inline-code], and want to be explicit

[#git-push-origin-flags][.inline-code]git push origin[.inline-code] flags[#git-push-origin-flags]

[.inline-code]git push origin <flag>[.inline-code]:

  • [.inline-code]-u[.inline-code], or [.inline-code]--set-upstream[.inline-code]:
  • 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.
  • [.inline-code]-f[.inline-code], [.inline-code]--force[.inline-code]:
  • 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! 
  • [.inline-code]-d[.inline-code], [.inline-code]--delete[.inline-code]:
  • Deletes the remote branches listed:
  • [.inline-code]git push origin --delete <branch name>[.inline-code] 
  • [.inline-code]--all[.inline-code]:
  • 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 [.inline-code]HEAD[.inline-code] to grab the [.inline-code]<branchname>[.inline-code] of the current branch without having to type it out. The [.inline-code]git push origin HEAD[.inline-code] command will push the current branch to the remote counterpart on github or some other server, if it exists.

[#no-upstream-branch-error]How to solve the [.inline-code]no upstream branch[.inline-code] error message[#no-upstream-branch-error]


 $ git push --set-upstream origin 

To create a remote branch with the same name as the local branch and push changes to the remote branch (aka, “set upstream”).

[#still-not-working]Help! [.inline-code]git push origin[.inline-code] still isn’t working![#still-not-working]

Try these troubleshooting tips:

  • Check that you are on the branch you want to push using [.inline-code]git status[.inline-code]
  • If using remote and local repository names or references explicitly, make sure they are spelled correctly