What does git remote add origin

2.5 Git Basics - Working with Remotes

Work with remotes

To be able to work on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the Internet or somewhere on the network. You can set up several, each of which is usually either read-only or writable to you. Working with others involves managing these remote repositories and pushing and pulling data to and from the repositories when you want to share your work. Managing remote repositories includes knowing how to add remote repositories, remove obsolete remotes, manage various remote branches, define them as versioned or unversioned, and much more. In this section we will discuss some of these remote management skills.

Remote repositories can also be located on your local computer.

It is entirely possible that you can work with a “remote” repository that is actually on the same host that you are currently working on. The word “remote” does not necessarily mean that the repository is in a different location on the network or the Internet, just that it is in a different location. Working with such a remote repository would still involve all of the usual push, pull, and fetch operations as with any other remote repository.

List the remotes

To see which remote servers you have configured, you can invoke the command. It lists the short names of each remote handle you have specified. If you've cloned your repository, you should at least see - this is the default name Git gives the server you cloned from:

You can also specify that shows you the URLs that Git has saved for the short name to be used when reading and writing on this remote:

If you have more than one remote, the command will list them all. For example, a repository with multiple remotes for multi-party work might look like this.

That means we can get posts from any of these users pretty easily. We may also have permission to push on one or more of these, although we cannot see that here.

Adding remote repositories

We have already mentioned and shown a few examples of how the command silently adds the remote for you. This is how you can explicitly add a new remote. To add a new remote Git repository as a short name that you can easily refer to, run:

Now you can use the string on the command line instead of the entire url. For example, if you want to get all of the information Paul has but not yet in your repository, you can run:

Paul's branch is now locally accessible - you can integrate it into one of your branches or you can switch to a local branch at this point (checkout) if you want to inspect it. (We'll go into more detail in Git Branching on what branches are and how to use them much more precisely.)

Fetch and pull your remotes

As you just saw, you can get data from your remote projects:

The command goes to the remote project and pulls down all data from this remote project that you do not have yet. After that you should have references to all branches from this remote, which you can integrate or inspect at any time.

When you clone a repository, the command automatically adds that remote repository with the name "origin". This fetches all new content that has been moved to this server since cloning (or the last time it was fetched). It's important to note, however, that the command only downloads the data to your local repository - it doesn't automatically merge it with your work or change what you're working on. You'll have to manually merge it with your work when you're done.

If your current branch is set up to track a remote branch, you can use the command to automatically fetch that remote branch and then merge it with your current branch (see the next section and Git branching for more Information). This could be a simpler or more convenient workflow for you. By default, the command will automatically set up your local branch to version the remote branch (or whatever the default branch is called) on the server you cloned it from. Usually when you run it pulls data from the server you originally cloned from and automatically tries to merge into the code you are currently working on.

Starting with version 2.27 of Git, a warning is issued if the variable is not set. Git will warn you until you set the variable.

If you want to keep the standard behavior (if possible a fast-forward, otherwise create a merge commit) of Git:

If you want to do a rebase with pulling:

Push to your remotes

When you have your project at a certain point that you want to share, you'll need to push it upstream. The command for this is simple:. If you want to move your branch to your server (again, cloning usually sets up both of these names for you automatically), you can also use this command to transfer all commits you have made to the server:

However, this command only works if you have cloned from a server to which you have write access and if nobody else has pushed in the meantime. If you and another user are cloning at the same time and you both want to push upstream, but you push upstream a little later, then your push will be rejected, and rightly so. You must first pick up its processing and incorporate it into yours before you can push. See Chapter 3 Git Branching for more details on pushing to remote servers.

Inspect a remote

If you want to see more information about a specific remote, you can use the command. If you run this command with a specific nickname, such as, you will get a similar message:

It lists the URL for the remote repository and the tracking industry information. The command helpfully tells you that if you are in the master branch and running, the branch of the remote will be automatically merged with the local branch after it has been fetched. It also lists any remote references that it has obtained.

This is just a simple example that you are likely to encounter. On the other hand, if you use Git more intensively, you can get a lot more information from:

This command shows which branch will be automatically pushed to if you run while in certain branches. It also shows you which remote branches are on the server that you don't already have, which remote branches you have but which have been removed from the server, and the local branches that are automatically merged (merged) with your remote tracking branch can if you run.

Renaming and Removing Remotes

You can run to change the short name of a remote. For example, if you want to rename to, you can do this with the command:

It should be noted that this will also change all of your remote tracking branch names. What was mentioned earlier is now.

If you want to remove a remote for any reason - you have moved the server, or are no longer using a particular mirror, or a contributor is no longer on it - then you can either use or:

Once you have deleted the reference to a remote in this way, all remote tracking branches and configuration settings associated with that remote will also be deleted.