[git] What is "origin" in Git?

When I run:

git push origin branchname

What exactly is origin and why do I have to type it before the branch name?

This question is related to git

The answer is


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


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.


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:

enter image description here

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'. enter image description here

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?

enter image description here

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.

enter image description here

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.


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).


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.


From 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.

Note that origin is by no means a "magical" name, but just a standard convention. Although it makes sense to leave this convention untouched, you could perfectly rename it without losing any functionality.

In the following example, the URL parameter to the "clone" command becomes the "origin" for the cloned local repository:

git clone https://github.com/gittower/git-crash-course.git

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.


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


origin is the default alias to the URL of your remote repository.


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.


The top answer is great.

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:

  • GitHub
  • A server to host your app

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

Example

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

Summary or 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.

As for branchname

branchname is simply what you're pushing 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.

  • Read more in the docs for git push by running: git push --help

When you clone a repository with git clone, it automatically creates a remote connection called origin pointing back to the cloned repository. This is useful for developers creating a local copy of a central repository since it provides an easy way to pull upstream changes or publish local commits. This behavior is also why most Git-based projects call their central repository origin.