Coding

git push origin main: What Each Part of the Command Means

git push origin main uploads the commits on your local main branch to the remote repository named origin, and updates the main branch there. It is shorthand: origin is the remote, main is the branch, and Git fills in the rest.

The command has a fixed shape, git push <repository> <refspec>. Once you know what fills each slot, you can change the target, push a branch under a different name, and read the errors without guessing. Everything below comes from the git-push reference and the Pro Git book. If you only need the steps to get a project onto GitHub, see how to push to GitHub.

What each word means

PartMeaning
git pushSend commits from your local repository to another repository.
originThe <repository> argument: the name of a remote, which is a saved shortcut for a URL.
mainThe <refspec> argument: here, the name of the local branch to push.

The git-push documentation defines the repository argument as the "remote" repository that is the destination of the push. It can be a URL, the name of a remote, or the name of a remote group. In nearly every project it is a remote name.

What origin is

A remote is a name Git stores next to a URL. You can see yours with:

git remote -v

Example output:

origin  https://github.com/alex/demo.git (fetch)
origin  https://github.com/alex/demo.git (push)

The Pro Git book says that "origin" has no special meaning in Git, just as "master" does not. It is the default name git clone gives the remote you cloned from, and git clone -o booyah would call it booyah instead. If you started with git init and added the remote yourself, origin exists only because you ran git remote add origin URL.

That also explains the most common surprise. If your repository has a remote with another name, or none at all, git push origin main has nothing to resolve the word origin against. Check git remote -v first.

What main is

main is the name of a branch, and like origin it is only a convention. The Pro Git book notes that "master" is the default name for a starting branch when you run git init, which is the only reason it is widely used. Many hosts and teams now default to main, but your repository may use either name or a different one. List your local branches with git branch, and use the name that matches the one you want to publish.

Technically the argument is a refspec, not a branch name. The documentation gives the format as [+]<src>[:<dst>]. The source is usually a local branch name, though it can be any "SHA-1 expression". The destination must be the name of a branch, tag or other ref on the remote side.

What Git expands the command to

When you give a single name with no colon, the documentation says Git updates the same ref as the source. A branch called main expands to main:refs/heads/main. So these two commands do the same thing:

git push origin main
git push origin main:refs/heads/main

Read it as "take my main and make it the remote's main". The Pro Git book uses the same wording for its serverfix example, and notes that the colon form lets you push a local branch to a remote branch with a different name:

git push origin main:release-candidate

That sends your local main to a remote branch called release-candidate. Your local branch name does not change. The leading + in a refspec has the same effect as --force, so avoid pasting refspecs that begin with it unless you mean to overwrite the remote branch.

What happens when you leave parts out

The documentation says the repository argument defaults to the upstream for the current branch, or to origin if there is no configured upstream. That is why a bare git push often works. What gets pushed in that case depends on the push.default setting. Its simple mode, the default since Git 2.0, pushes the current branch to the branch with the same name on the remote. When you push back to the remote you pull from, it also requires the current branch to have an upstream with the same name.

Spelling out origin main removes that dependence on configuration. It pushes the branch you named to the remote you named, whichever branch is checked out. That is useful in scripts, in documentation, and when you are unsure what a branch tracks.

What -u adds

You will often see git push -u origin main. The -u flag is short for --set-upstream. The documentation describes it as adding an upstream (tracking) reference for every branch that is up to date or successfully pushed, used by argument-less git pull and other commands.

The Pro Git book explains the result: a local branch with a direct relationship to a remote branch is a tracking branch, and the branch it tracks is its upstream. On a tracking branch, git pull knows which server to fetch from and which branch to merge. You only need -u once per branch. To see what a branch currently tracks, run git branch -vv. The difference between fetching and pulling is covered in git fetch vs git pull.

What gets updated locally after a push

Besides sending commits, a successful push updates your remote-tracking branch, origin/main. The book describes remote-tracking branches as local references you cannot move yourself. Git moves them whenever you communicate over the network, so they act as bookmarks for where the remote branches were the last time you connected. That bookmark is what git log origin/main and git status compare against when they say your branch is ahead of or behind the remote.

Why the push can be refused

The git-push documentation says Git usually refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. In plain terms: if the remote main has commits that your local main lacks, your push would discard them, so Git stops. Bring those commits in first, then push again:

git pull origin main
git push origin main

If you and a teammate rewrote history differently, the choice between merging and rebasing is explained in git merge vs rebase.

When forcing is on the table

--force-with-lease overrides that refusal only if the current value of the remote ref is the value you expect. Used alone, it requires the remote ref to match your remote-tracking branch, and the push fails otherwise. The documentation also notes that every form except the one naming the expected value explicitly, --force-with-lease=<refname>:<expect>, is still considered experimental. Treat forcing as something you do on a branch only you use, after rewriting your own commits, and never as a way around a rejection on a shared main.

git push --force-with-lease origin main

Other branches and other remotes

The same two slots work for any branch and any remote. To publish a feature branch, replace main with its name, for example git push -u origin my-feature. To send the same branch to a second remote, replace origin with that remote's name. The branch-creation steps are in git checkout -b, and removing a branch afterwards is in how to delete a Git branch.

If git push origin main still fails

Work through three checks in order. Run git remote -v to confirm that origin exists and points at the URL you expect. Run git branch to confirm that a local branch called exactly main exists, since a branch named master will not match it. Then run git status to see whether you have uncommitted work, because a push sends commits only, not edits you have not committed. If the checks pass and the remote still refuses, read the message Git prints: it names the ref it rejected and usually says why.

Frequently asked questions

Is git push origin main the same as git push?
Only when your current branch is main and it has an upstream. The git-push documentation says the repository defaults to the upstream of the current branch, or to origin if no upstream is configured. A plain git push then uses the push.default setting, which is simple since Git 2.0 and pushes the current branch to the same name on the remote.
Does origin have to be called origin?
No. The Pro Git book says origin has no special meaning in Git. It is only the default name git clone gives to the remote you cloned from. You can name a remote anything, and git clone -o NAME picks a different default.
What does git push origin main:other do?
It pushes your local main branch to a remote branch named other. The part before the colon is the source, the part after it is the destination ref on the remote. Without a colon, Git expands main to main:refs/heads/main.
Should I add -u to git push origin main?
Add it the first time you push a branch. The -u flag (--set-upstream) records an upstream tracking reference, which argument-less git pull and other commands use. After that, a plain git push from the branch knows where to go.
Is git push origin main safe to repeat?
Yes. If the remote already has your commits, there is nothing to send. If the remote has commits you lack, Git refuses the push instead of overwriting them, unless you force it.

Sources