Reputation: 30421
When I run:
git push origin branchname
What exactly is origin
and why do I have to type it before the branch name?
Upvotes: 782
Views: 372041
Reputation: 360
I for one got stuck on this early. Consider
git push origin master
git log origin..
Both are common idioms but the term origin is not being used the same way. The first is used as a remote alias, the second as a remote ref, more explicity written as git log origin/master~0..master~0
both of which mean give me what's in master not in origin/master with respect to the last fetch; however, it looks for the remote-ref head and branch head to make that comparison by default and in the first case assume the current checked out branch too.
The second command is invalid without that remote ref and the remote ref would be invalid without that remote but in practical use, the term origin can have this meaning too, or any other remote for that matter.
Upvotes: 1
Reputation: 52268
I would just add, that it becomes easy to understand if you think about remotes as locations other than your computer that you may want to move your code to.
Some very good examples are:
So you can certainly have multiple remotes. A very common pattern is to use GitHub to store your code, and a server to host your application (if it's a web application). Then you would have 2 remotes (possibly more if you have other environments).
Try opening your git config by typing git config -e
Note: press escape, then :, then q then enter to quit out
Here's what you might see in your git configs if you had 3 remotes. In this example, 1 remote (called 'origin') is GitHub, another remote (called 'staging') is a staging server, and the third (called 'heroku') is a production server.
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/username/reponame.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[remote "heroku"]
url = https://git.heroku.com/appname.git
fetch = +refs/heads/*:refs/remotes/heroku/*
[remote "staging"]
url = https://git.heroku.com/warm-bedlands-98000.git
fetch = +refs/heads/*:refs/remotes/staging/*
The three lines starting with [remote ...
show us the remotes we can push to.
Running git push origin
will push to the url for '[remote "origin"]', i.e. to GitHub
But similarly, we could push to another remote, say, '[remote "staging"]', with git push staging
, then it would push to https://git.heroku.com/warm-bedlands-98000.git
.
In the example above, we can see the 3 remotes with git remote
:
git remote
heroku
origin
staging
origin
Remotes are simply places on the internet that you may have a reason to send your code to. GitHub is an obvious place, as are servers that host your app, and you may have other locations too. git push origin
simply means it will push to 'origin', which is the name GitHub chooses to default to.
branchname
branchname
is simply what you're push
ing to the remote. According the git push help docs, the branchname
argument is technically a refspec
, which, for practical purposes, is the branch you want to push.
git push
by running: git push --help
Upvotes: 8
Reputation: 1531
Origin
is the shortname that acts like an alias for the url of the remote repository.
Let me explain with an example.
Suppose you have a remote repository
called amazing-project
and then you clone that remote repository to your local machine so that you have a local repository
. Then you would have something like what you can see in the diagram below:
Because you cloned the repository. The remote repository and the local repository are linked.
If you run the command git remote -v
it will list all the remote repositories that are linked to your local repository. There you will see that in order to push or fetch code from your remote repository you will use the shortname 'origin'.
Now, this may be a bit confusing because in GitHub (or the remote server) the project is called 'amazing-project'. So why does it seem like there are two names for the remote repository?
Well one of the names that we have for our repository is the name it has on GitHub or a remote server somewhere. This can be kind of thought like a project name. And in our case that is 'amazing-project'.
The other name that we have for our repository is the shortname that it has in our local repository that is related to the URL of the repository. It is the shortname we are going to use whenever we want to push or fetch code from that remote repository. And this shortname kind of acts like an alias for the url, it's a way for us to avoid having to use that entire long url in order to push or fetch code. And in our example above it is called origin
.
So, what is origin
?
Basically origin is the default shortname that Git uses for a remote repository when you clone that remote repository. So it's just the default.
In many cases you will have links to multiple remote repositories in your local repository and each of those will have a different shortname.
So final question, why don't we just use the same name?
I will answer that question with another example. Suppose we have a friend who forks our remote repository so they can help us on our project. And let's assume we want to be able to fetch code from their remote repository. We can use the command git remote add <shortname> <url>
in order to add a link to their remote repository in our local repository.
In the above image you can see that I used the shortname friend
to refer to my friend's remote repository. You can also see that both of the remote repositories have the same project name amazing-project
and that gives us one reason why the remote repository names in the remote server and the shortnames in our local repositories should not be the same!
There is a really helpful video πΉ that explains all of this that can be found here.
Upvotes: 47
Reputation: 2368
remote(repository url alias) β origin(upstream alias) β master(branch alias);
remote
, level same as working directory
, index
, repository
,
origin
, local repository branch map to remote repository branch
Upvotes: 1
Reputation: 61
The other answers say that origin
is an alias for the URL of a remote repository which is not entirely accurate. It should be noted that an address that starts with http
is a URL while one that starts with git@
is a URI or Universal Resource Identifier.
All URLs are URIs, but not all URIs are URLs.
In short, when you type git remote add origin <URI>
you are telling your local git that whenever you use the word origin
you actually mean the URI that you specified. Think of it like a variable holding a value.
And just like a variable, you can name it whatever you want (eg. github
, heroku
, destination
, etc).
Upvotes: 3
Reputation: 1262
The best answer here:
https://www.git-tower.com/learn/git/glossary/origin
In Git, "origin" is a shorthand name for the remote repository that a project was originally cloned from. More precisely, it is used instead of that original repository's URL - and thereby makes referencing much easier.
Upvotes: 7
Reputation: 4116
Simple! "origin" is just what you nicknamed your remote repository when you ran a command like this:
git remote add origin [email protected]:USERNAME/REPOSITORY-NAME.git
From then on Git knows that "origin" points to that specific repository (in this case a GitHub repository). You could have named it "github" or "repo" or whatever you wanted.
Upvotes: 33
Reputation: 9924
origin
is not the remote repository name. It is rather a local alias set as a key in place of the remote repository URL.
It avoids the user having to type the whole remote URL when prompting a push.
This name is set by default and for convention by Git when cloning from a remote for the first time.
This alias name is not hard coded and could be changed using following command prompt:
git remote rename origin mynewalias
Take a look at http://git-scm.com/docs/git-remote for further clarifications.
Upvotes: 207
Reputation: 19021
Git has the concept of "remotes", which are simply URLs to other copies of your repository. When you clone another repository, Git automatically creates a remote named "origin" and points to it.
You can see more information about the remote by typing git remote show origin
.
Upvotes: 85
Reputation: 19682
origin
is an alias on your system for a particular remote repository. It's not actually a property of that repository.
By doing
git push origin branchname
you're saying to push to the origin
repository. There's no requirement to name the remote repository origin
: in fact the same repository could have a different alias for another developer.
Remotes are simply an alias that store the URL of repositories. You can see what URL belongs to each remote by using
git remote -v
In the push
command, you can use remotes or you can simply use a URL directly. An example that uses the URL:
git push [email protected]:git/git.git master
Upvotes: 766
Reputation: 249
I was also confused by this, and below is what I have learned.
When you clone a repository, for example from GitHub:
origin
is the alias for the URL from which you cloned the repository. Note that you can change this alias.
There is one master
branch in the remote repository (aliased by origin
). There is also another master
branch created locally.
Further information can be found from this SO question: Git branching: master vs. origin/master vs. remotes/origin/master
Upvotes: 24
Reputation: 2854
origin
is the default alias to the URL of your remote repository.
Upvotes: 54