Unlike centralized version control systems that have a client that is very different from a server, Git repositories are all basically equal and you simply synchronize between them. This makes it easy to have more than one remote repository – you can have some that you have read-only access to and others that you can write to as well.
So that you don’t have to use the full URL of a remote repository every time you want to synchronize with it, Git stores an alias or nickname for each remote repository URL you are interested in. You use the git remote
command to manage this list of remote repos that you care about.
git remote list your remote aliases
Without any arguments, Git will simply show you the remote repository aliases that it has stored. By default, if you cloned the project (as opposed to creating a new one locally), Git will automatically add the URL of the repository that you cloned from under the name ‘origin’. If you run the command with the -v
option, you can see the actual URL for each alias.
$ git remote
origin
$ git remote -v
origin git@github.com:github/git-reference.git (fetch)
origin git@github.com:github/git-reference.git (push)
You see the URL there twice because Git allows you to have different push and fetch URLs for each remote in case you want to use different protocols for reads and writes.
git remote add add a new remote repository of your project
If you want to share a locally created repository, or you want to take contributions from someone else’s repository – if you want to interact in any way with a new repository, it’s generally easiest to add it as a remote. You do that by running git remote add [alias] [url]
. That adds [url]
under a local remote named [alias]
.
For example, if we want to share our Hello World program with the world, we can create a new repository on a server (Using GitHub as an example), which should give you a URL, in this case “git@github.com:schacon/hw.git”. To add that to our project so we can push to it and fetch updates from it we would do this:
$ git remote
$ git remote add github git@github.com:schacon/hw.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
Like the branch naming, remote alias names are arbitrary – just as ‘master’ has no special meaning but is widely used because git init
sets it up by default, ‘origin’ is often used as a remote name because git clone
sets it up by default as the cloned-from URL. In this case we’ll name the remote ‘github’, but you could name it just about anything.
git remote rm removing an existing remote alias
Git addeth and Git taketh away. If you need to remove a remote – you are not using it anymore, the project is gone, etc – you can remove it with git remote rm [alias]
.
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
$ git remote add origin git://github.com/pjhyett/hw.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/pjhyett/hw.git (fetch)
origin git://github.com/pjhyett/hw.git (push)
$ git remote rm origin
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
git remote rename [old-alias] [new-alias] rename remote aliases
If you want to rename remote aliases without having to delete them and add them again you can do that by running git remote rename [old-alias] [new-alias]
. This will allow you to modify the current name of the remote.
$ git remote add github git@github.com:schacon/hw.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
$ git remote rename github origin
$ git remote -v
origin git@github.com:schacon/hw.git (fetch)
origin git@github.com:schacon/hw.git (push)
In a nutshell with git remote
you can list our remote repositories and whatever URL that repository is using. You can use git remote add
to add new remotes, git remote rm
to delete existing ones or git remote rename [old-alias] [new-alias]
to rename them.
git remote set-url update an existing remote URL
Should you ever need to update a remote’s URL, you can do so with the git remote set-url
command.
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/pjhyett/hw.git (fetch)
origin git://github.com/pjhyett/hw.git (push)
$ git remote set-url origin git://github.com/github/git-reference.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/github/git-reference.git (push)
In addition to this, you can set a different push URL when you include the --push
flag. This allows you to fetch from one repo while pushing to another and yet both use the same remote alias.
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/github/git-reference.git (push)
$ git remote set-url --push origin git://github.com/pjhyett/hw.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/pjhyett/hw.git (push)
Internally, the git remote set-url
command calls git config remote
, but has the added benefit of reporting back any errors. git config remote
on the other hand, will silently fail if you mistype an argument or option and not actually set anything.
For example, we’ll update the github
remote but instead reference it as guhflub
in both invocations.
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/github/git-reference.git (push)
$ git config remote.guhflub git://github.com/mojombo/hw.git
$ git remote -v
github git@github.com:schacon/hw.git (fetch)
github git@github.com:schacon/hw.git (push)
origin git://github.com/github/git-reference.git (fetch)
origin git://github.com/github/git-reference.git (push)
$ git remote set-url guhflub git://github.com/mojombo/hw.git
fatal: No such remote 'guhflub'
In a nutshell, you can update the locations of your remotes with git remote set-url
. You can also set different push and fetch URLs under the same remote alias.
You must be logged in to post a comment.