The first push should be a:

git push -u origin branchname

That would make sure:

  • your local branch has a remote tracking branch of the same name referring an upstream branch in your remote repo 'origin',
  • this is compliant with the default push policy 'simple'

Any future git push will, with that default policy, only push the current branch, and only if that branch has an upstream branch with the same name.
that avoid pushing all matching branches (previous default policy), where tons of test branches were pushed even though they aren't ready to be visible on the upstream repo.

Answer from VonC on Stack Overflow
🌐
Git
git-scm.com › docs › git-push
Git - git-push Documentation
The <repository> argument defaults to the upstream for the current branch, or origin if there’s no configured upstream. To decide which branches, tags, or other refs to push, Git uses (in order of precedence):
Discussions

Can't push branch into origin main
When you specify a branch to push, git push origin main, it pushes to an origin branch whose name depends on your local config; the factory default for this is to a branch with the same name. You hadn't done any work on your own main, and the origin's main also hasn't changed, so you're "Already up to date." If you want to push your checked out branch to origin's main, git push origin @:main and you're starting to get into " refspecs ", specifying reference mappings when pushing and fetching between repos. Git's configurable and flexible almost (but not quite) to a fault The short form is, unadorned names for things in your repos are "refnames", and except for really special refnames like HEAD, their full spelling always starts refs/. Branch refnames start refs/heads/, tag refnames start refs/tags/, and the full spelling of a refname is how Git knows what kind it is. When you give a refname, git follows some rules to make shorthand like main work: If $GIT_DIR/ exists, that is what you mean (this is usually useful only for HEAD, FETCH_HEAD, ORIG_HEAD, MERGE_HEAD and CHERRY_PICK_HEAD); otherwise, refs/ if it exists; otherwise, refs/tags/ if it exists; otherwise, refs/heads/ if it exists; otherwise, refs/remotes/ if it exists; otherwise, refs/remotes//HEAD if it exists. So, since from comments you have local versions of both branch names, git push origin main got translated by the standard revision parsing to git push origin refs/heads/main, then git push saw that you had given an explicit remote but not a full refspec, looked up your push.default setting and if it's still at the factory setting found simple, which ... well, it's simple in terms of how much explaining isn't necessary right up front, not so simple in terms of what it does to avoid some common newbie mistakes without interfering with normal workflows . It boils down to at factory defaults acting as if you'd typed git push origin main:main. More on reddit.com
🌐 r/git
15
4
May 12, 2021
github - What is "git remote add ..." and "git push origin master"? - Stack Overflow
Quite often, Git and Ruby on Rails looks like magic... such as in the first chapter of Ruby on Rails 3 Tutorial book, it talks about Git: git remote add origin git@github.com:peter/first_app.git gi... More on stackoverflow.com
🌐 stackoverflow.com
What's the meaning of 'origin' in 'git push origin master' - Stack Overflow
When I run: git push origin master ...what is the meaning of origin in this context? More on stackoverflow.com
🌐 stackoverflow.com
"git push origin" problem
Application Name: on project Open Concept Lab Question: every time I try to push my code to my git repo it gives this error my local repo is also up to date I don’t know, from when I started using “git stash” the problem started to occure More on talk.openmrs.org
🌐 talk.openmrs.org
5
1
February 7, 2021
🌐
Warp
warp.dev › terminus › understanding-git-push-origin
Warp: Understanding Git Push Origin
January 31, 2024 - Where git push initiates the push, origin refers to the remote counterpart of the project, and main is the branch name.
🌐
Graphite
graphite.com › guides › how-to-use-git-push-origin
How to use git push origin - Graphite
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.
🌐
Atlassian
atlassian.com › git › tutorials › syncing › git-push
Git Push | Atlassian Git Tutorial
The interactive rebase is also a good opportunity to clean up your commits before sharing them. Then, the git push command sends all of the commits on your local main to the central repository. 1git checkout main 2git fetch origin main 3git rebase -i origin/main 4# Squash commits, fix up commit ...
🌐
Nick Janetakis
nickjanetakis.com › blog › use-git-push-origin-head-to-quickly-push-the-checked-out-branch
Use git push origin HEAD to Quickly Push the Checked Out Branch — Nick Janetakis
September 30, 2025 - I use git from the command line most of the time and when working on branches that I plan to push and make a PR out of, I found myself typing git push origin 123-hello-world or whatever the branch name happens to be to push changes.
Find elsewhere
🌐
CoreUI
coreui.io › answers › how-to-push-changes-in-git
How to push changes in Git · CoreUI
October 3, 2025 - The syntax git push origin main specifies the remote name (origin) and branch name (main). If you’ve set up tracking between local and remote branches, you can simply use git push without additional parameters.
🌐
GeeksforGeeks
geeksforgeeks.org › git › difference-between-git-push-origin-and-git-push-origin-master
Difference Between Git Push Origin and Git Push Origin Master - GeeksforGeeks
September 18, 2024 - The difference between git push origin and git push origin master lies in their flexibility and context dependency. git push origin is useful when you have set upstream branches and want a shorter, context-aware command. In contrast, git push origin master is simple and explicitly targets the master branch.
🌐
Reddit
reddit.com › r/git › can't push branch into origin main
r/git on Reddit: Can't push branch into origin main
May 12, 2021 -

Hello,

I've made a repo in which I have just two branches:

  • MAIN

  • NPM

I do all my work on NPM, and I want to push directly to my MAIN branch once I'm satisfied with the work. Once I do git push origin main from the NPM branch, it just tells me that "Everything up-to-date", whereas when I go back to my MAIN branch, all the changes that I've done were not appended (I've already add, commited).

Seems as if it's still not pushing. When I go into my repo, NPM isn't there as well.

Top answer
1 of 3
3
When you specify a branch to push, git push origin main, it pushes to an origin branch whose name depends on your local config; the factory default for this is to a branch with the same name. You hadn't done any work on your own main, and the origin's main also hasn't changed, so you're "Already up to date." If you want to push your checked out branch to origin's main, git push origin @:main and you're starting to get into " refspecs ", specifying reference mappings when pushing and fetching between repos. Git's configurable and flexible almost (but not quite) to a fault The short form is, unadorned names for things in your repos are "refnames", and except for really special refnames like HEAD, their full spelling always starts refs/. Branch refnames start refs/heads/, tag refnames start refs/tags/, and the full spelling of a refname is how Git knows what kind it is. When you give a refname, git follows some rules to make shorthand like main work: If $GIT_DIR/ exists, that is what you mean (this is usually useful only for HEAD, FETCH_HEAD, ORIG_HEAD, MERGE_HEAD and CHERRY_PICK_HEAD); otherwise, refs/ if it exists; otherwise, refs/tags/ if it exists; otherwise, refs/heads/ if it exists; otherwise, refs/remotes/ if it exists; otherwise, refs/remotes//HEAD if it exists. So, since from comments you have local versions of both branch names, git push origin main got translated by the standard revision parsing to git push origin refs/heads/main, then git push saw that you had given an explicit remote but not a full refspec, looked up your push.default setting and if it's still at the factory setting found simple, which ... well, it's simple in terms of how much explaining isn't necessary right up front, not so simple in terms of what it does to avoid some common newbie mistakes without interfering with normal workflows . It boils down to at factory defaults acting as if you'd typed git push origin main:main.
2 of 3
2
What happens when you pull your main branch? Note that your workflow probably works for a single, disciplined developer, but is bound to get you in trouble in more complicated scenarios. Normally you would not push a branch directly to a different branch on the server. There are multiple ways to get your work from one branch to another - merge, rebase, or pull request are three common examples. It sounds like your changes are in the main branch on the server, and should appear in your local main branch after you pull them back from the server.
Top answer
1 of 6
398

Git is like Unix. It is user-friendly, but it is picky about its friends. It's about as powerful and as user-friendly as a shell pipeline.

That being said, once you understand its paradigms and concepts, it has the same Zenlike clarity that I've come to expect from Unix command-line tools. You should consider taking some time off to read one of the many good Git tutorials available online. The Pro Git book is a good place to start.

To answer your first question.

  1. What is git remote add ...?

    As you probably know, Git is a distributed version control system. Most operations are done locally. To communicate with the outside world, Git uses what are called "remotes". These are repositories other than the one on your local disk which you can push your changes into (so that other people can see them) or pull from (so that you can get others changes). The command git remote add origin [email protected]:peter/first_app.git creates a new remote called origin located at [email protected]:peter/first_app.git. Once you do this, in your push commands, you can push to origin instead of typing out the whole URL.

  2. What is git push origin master?

    This is a command that says "push the commits in the local branch named master to the remote named origin". Once this is executed, all the stuff that you last synchronised with origin will be sent to the remote repository and other people will be able to see them there.

Now about transports (i.e., what git://) means. Remote repository URLs can be of many types (file://, https://, etc.). Git simply relies on the authentication mechanism provided by the transport to take care of permissions and stuff. This means that for file:// URLs, it will be Unix file permissions, etc. The git:// scheme is asking Git to use its own internal transport protocol, which is optimised for sending Git changesets around. As for the exact URL, it's the way it is because of the way GitHub has set up its Git server.

Now the verbosity. The command you've typed is the general one. It's possible to tell Git something like "the branch called master over here is local mirror of the branch called foo on the remote called bar". In Git speak, this means that master tracks bar/foo. When you clone for the first time, you will get a branch called master and a remote called origin (where you cloned from) with the local master set to track the master on origin.

Once this is set up, you can simply say git push and it'll do it. The longer command is available in case you need it (e.g., git push might push to the official public repository and git push review master can be used to push to a separate remote which your team uses to review code). You can set your branch to be a tracking branch using the --set-upstream option of the git branch command.

I've felt that Git (unlike most other applications I've used) is better understood from the inside out. Once you understand how data is stored and maintained inside the repository, the commands and what they do become crystal clear. I do agree with you that there's some elitism amongst many Git users, but I also found that with Unix users once upon a time, and it was worth ploughing past them to learn the system. Good luck!

2 of 6
44

Update: note that the currently accepted answer perpetuates a common misunderstanding about the behaviour of git push, which hasn't been corrected despite a comment pointing it out.

Your summary of what remotes are - like a nickname for the URL of a repository - is correct.

So why does the URL not git://[email protected]/peter/first_app.git, but in the other syntax -- what syntax is it? Why must it end with .git? I tried not using .git at the end and it works too. If not .git, what else can it be? The git at the beginner seems to be a user account on the Git server?

The two URLs that you've mentioned indicate that two different transport protocols should be used. The one beginning with git:// is for the Git protocol, which is usually only used for read-only access to repositories. The other one, [email protected]:peter/first_app.git, is one of the different ways of specifying access to a repository over SSH - this is the "scp-style syntax" described in the documentation. That the username in the scp-style syntax is git is because of the way that GitHub deals with identifying users - essentially that username is ignored, and the user is identified based on the SSH key-pair that they used to authenticate.

As for the verbosity of git push origin master, you've noticed that after the first push, you can then just do git push. This is because of a series of difficult-to-remember-but-generally-helpful defaults :)

  • If no remote is specified, the remote configured for the current branch (in remote.master.url in your case) is used. If that's not set up, then origin is used.
  • If there's no "refspec" (e.g. master, master:my-experiment, etc.) specified, then Git defaults to pushing every local branch that has the same name as a branch on the remote. If you just have a branch called master in common between your repository and the remote one, that'll be the same as pushing your master to the remote master.

Personally, since I tend to have many topic branches (and often several remotes) I always use the form:

git push origin master

... to avoid accidentally pushing other branches.


In reply to your comments on one of the other answers, it sounds to me as if are learning about Git in a top-down way very effectively - you've discovered that the defaults work, and your question is asking about why ;) To be more serious, Git can be used essentially as simply as SVN, but knowing a bit about remotes and branches means you can use it much more flexibly and this can really change the way you work for the better.

Your remark about a semester course makes me think of something Scott Chacon said in a podcast interview - students are taught about all kinds of basic tools in computer science and software engineering, but very rarely version control. Distributed version control systems such as Git and Mercurial are now so important, and so flexible, that it would be worth teaching courses on them to give people a good grounding.

My view is that with git, this learning curve is absolutely worth it - working with lots of topic branches, merging them easily, and pushing and pulling them about between different repositories is fantastically useful once you become confident with the system. It's just unfortunate that:

  • The primary documentation for Git is so hard to parse for newcomers. (Although I'd argue that if you google for almost any Git question, helpful tutorial material (or Stack Overflow answers :)) come up nowadays.)
  • There are a few odd behaviours in Git that are hard to change now because many scripts may rely on them, but are confusing to people.
🌐
freeCodeCamp
freecodecamp.org › news › git-push-to-remote-branch-how-to-push-a-local-branch-to-origin
Git Push to Remote Branch – How to Push a Local Branch to Origin
April 26, 2021 - (some-branch)$ git push origin some-branch:my-feature Total 0 (delta 0), reused 0 (delta 0) To github.com:johnmosesman/burner-repo.git + 728f0df...8bf04ea some-branch -> my-feature
🌐
DEV Community
dev.to › gcristiantoma › comment › opma
Git push origin [branch-name] , I would like to ask, origin stands for ? Than... - DEV Community
May 13, 2020 - This is a great question Cristian! origin stands for your remote repository. So when you use "git push origin [branch-name]", you are saying push my code to a branch called [branch-name] in this specific remote repository called "origin".
🌐
Educative
educative.io › answers › what-is-the-git-push---force--u-origin-command
What is the git push --force -u origin command?
The git push --force -u origin command overrides this restriction in Git, meaning it allows you to forcefully overwrite the commit history of your local branch to the remote repository branch.
Top answer
1 of 3
439

The key is "argument-less git-pull". When you do a git pull from a branch, without specifying a source remote or branch, git looks at the branch.<name>.merge setting to know where to pull from. git push -u sets this information for the branch you're pushing.

To see the difference, let's use a new empty branch:

$ git checkout -b test

First, we push without -u:

$ git push origin test
$ git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.test.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "test"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

Now if we add -u:

$ git push -u origin test
Branch test set up to track remote branch test from origin.
Everything up-to-date
$ git pull
Already up-to-date.

Note that tracking information has been set up so that git pull works as expected without specifying the remote or branch.

Update: Bonus tips:

  • As Mark mentions in a comment, in addition to git pull this setting also affects default behavior of git push. If you get in the habit of using -u to capture the remote branch you intend to track, I recommend setting your push.default config value to upstream.
  • git push -u <remote> HEAD will push the current branch to a branch of the same name on <remote> (and also set up tracking so you can do git push after that).
2 of 3
125
git push -u origin master

… is the same as:

git push origin master ; git branch --set-upstream master origin/master

Do the last statement, if you forget the -u!

Or you could force it:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

If you let the command do it for you, it will pick your mistakes like if you typed a non-existent branch or you didn't git remote add; though that might be what you want. :)

🌐
Linux Kernel
kernel.org › pub › software › scm › git › docs › git-push.html
git-push(1) Manual Page
The <repository> argument defaults to the upstream for the current branch, or origin if there’s no configured upstream. To decide which branches, tags, or other refs to push, Git uses (in order of precedence):
🌐
Graphite
graphite.com › guides › git-push-u-origin-command-guide
Understanding the git command "git push -u origin"
The command git push -u origin is used to push changes from your local branch to the corresponding branch on the remote repository. The -u flag stands for --set-upstream, which links your local branch to a remote branch.