What is the difference between 'origin' and 'remote' in git commands?
GitGit RemoteGit Problem Overview
In git lingo, are origin
and remote
the same thing? Or does origin
refer to the local directory?
In the case of git push -u origin master
: Which of the following interpretation is correct?
- "push everything upstream to the remote repo called 'origin' and its branch 'master'"
- "push everything from the local originating repo called 'origin' to the upstream 'master' branch"
Appreciate any clarification!
The answers to my question clarified two issues for me:
origin
refers to the remote repo, rather than the local cloned copy of the remote repo. This is not clear when one reads thatorigin
is an alias ofremote
and is created at the time ofgit clone
origin
refers to the remote repo ingit push -u origin master
because local copies of the repo are implied and "rarely referenced".
Git Solutions
Solution 1 - Git
In git lingo origin
is just the default name for a remote from which a repo was originally cloned. It might equally have been called source
or remote1
or just remote
.
Remember that git
is a peer-to-peer, distributed system, not one with any built-in notion of client/server, master/slave, parent/child relationships (though these might be imposed upon it by a user in a particular scenario).
All remotes are equal. origin
is simply (and literally) the first among those equals (for a cloned repo). :)
And as Jan points out in the comments, the name associated with each remote is intended for your convenience. If you find that origin
does not really work for you then you can change it.
As for your interpretations of the push
statement, your first is the closest to being correct but the push command as written will push the local master
branch to the master
branch on the remote identified by the (locally configured) name origin
.
If there is no master
branch in the remote then one will be created.
Full details of the push command and the flags, options etc are of course in the docs.
You rarely (if ever) refer to the 'local' repo explicitly since your operations are performed in the context of a repo.
Solution 2 - Git
No, they don't mean the same thing.
remote
, in git
-speak, refers to any remote repository, such as your GitHub or another git
server.
origin
is the, by convention, default remote name in git
. When you do a git clone <url>
, <url>
is automatically added to your local repo under the name origin
. You can, of course, add other remotes under different names using git remote add
.
When you do git push -u origin master
, what it means is "push everything from my local master to the remote named origin
". The structure of this command is, of course, more general - the more general form is git push -u <remote> <branch>
, which will push the branch named branch
to the designated remote, creating it at the far end if the remote doesn't already have it (that's what the -u
flag does).
As a further addendum, git push
, by default, will push the current branch to origin
, corresponding to git push origin <current-branch>
.
Solution 3 - Git
You can have multiple remotes, each with a different name - such as the default "origin"
Your question makes the assumption that you currently have the master branch checked out.
git push -u origin master
Push the local branch named master to the "origin" remote as the branch named master. The -u flag tells local git to track that remote branch as upstream to your local branch.
Solution 4 - Git
A remote is just a word: a name to use to identify some other Git repository somewhere.
The string origin
is the default name of the (singular) remote that git clone
puts in automatically, when you clone from some other ("origin"-al) Git repository. You can choose some other name, and/or add more remotes. Each remote has, at the least, a URL, which is where the other repository is to be found.
For git push
, the third word—adjust this number if needed, if you add various flags; in this case you added -u
so now it's the fourth word—is normally the name of some remote.
The remaining words are all refspecs, which can be summarized (though not 100% accurately ... in fact, less than 50% accurately, depending on how you count :-) ) as a pair of branch names separated by a colon.
If you write just one branch name like master
, Git takes that to mean "use (part of) the upstream if one is set, otherwise use the same name after the colon." Usually the upstream, if set, has the same basic name, so master
usually winds up meaning master:master
. Git sends them—the Git at the remote's URL—commits found on your branch (the name on the left), and then asks them to set their branch (the name on the right) to the same tip commit you just pushed for that branch.
If you don't put in any refspecs, Git's default depends on your Git version. Since Git version 2.0, the default is to push your current branch to a branch of the same name on the remote.
The -u
flag tells git push
that, if the push succeeds, it should set the upstream for the branch you just pushed.
The upstream of a branch comes in two parts: the name of a remote, and the name of a branch on that remote. Since you gave git push
both items—the name of the remote was origin
, and the branch was the second (post-colon) master
from the implied master:master
from master
—this will, if it succeeds, set the upstream for master
to origin/master
.
(Edit: you might, quite legitimately, wonder where the /
came from in the upstream setting of origin/master
. That's partly a historical artifact. Unfortunately, it leads to huge amounts of confusion. For now, just keep in mind that remote, branch, and remote-tracking branch are all different things in Git. They're all related in various ways, but it's important to remember that they are not the same, and the terms have very specific meanings. The word track is also overloaded. The new term upstream is better, but not all descriptions use it.)
Solution 5 - Git
No, remote
is a parent structure of origin
. That's just the default name of the remote
Git creates when you clone a repository.
More information in this question: What is “origin” in Git?