日本語

How to Add, Change, and Remove Remotes with git remote

​​Git​command line

Use git remote add to register a remote under a name, git remote set-url to change its URL, and git remote remove to delete the registration. These are the operations for pushing a local repository to an empty repository you created on GitHub or elsewhere for the first time, for updating the URL after a repository has moved, and for switching a URL from HTTPS to SSH. I ran each command in a test environment and checked how the output of git remote -v and related commands changed before and after.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). For remotes, I used bare repositories created in the same environment (../old.git, ../new.git, and ../backup.git). For the https://example.com/... and git@example.com:... URLs, I only rewrote the registration and checked the display; I did not connect to them. I did not test pushing to GitHub or other hosting services, setting up SSH keys, or behavior on Windows and macOS.

The state before registering a remote

A repository created with git init has no remotes. git remote and git remote -v print nothing, and git push stops with a message that there is no destination.

console
$ git remote

$ git remote -v

$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

    git remote add <name> <url>

and then push using the remote name

    git push <name>

Register a remote with git remote add

Register a remote with git remote add <name> <URL>. The name can be anything, but git clone registers the source repository as origin (Pro Git, "Working with Remotes", in Japanese). I also used origin here.

console
$ git remote add origin ../old.git

$ git remote
origin

$ git remote -v
origin	../old.git (fetch)
origin	../old.git (push)

git remote lists only the names. With -v, it also shows the URL after each name (git remote reference). (fetch) is the URL used when fetching, and (push) is the URL used when pushing. Here the same URL appears on two lines.

The registration is written to the repository configuration (.git/config). Looking at it with git config, I found the URL and a setting that maps the fetched branches to origin/<branch name>.

console
$ git config --get-regexp "^remote\."
remote.origin.url ../old.git
remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

Registering a remote does not send anything yet. For the first push, adding -u sets main to track origin/main, so later you can push with just git push (git push reference).

console
$ git push -u origin main
To ../old.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

$ git branch -vv
* main 6b9961f [origin/main] 最初のコミット

$ git branch -r
  origin/main

When you see "remote origin already exists"

If you run git remote add with a name that already exists, it fails and nothing changes.

console
$ git remote add origin ../new.git
error: remote origin already exists.

If you only want to change the URL of origin, use set-url as described next. If you want to register another location as well, add it under a different name such as backup.

Change the URL with git remote set-url

The scenario is that the repository moved from ../old.git to ../new.git. The new location was created with git clone --bare, so the contents are the same. You can check the current URL with git remote get-url.

console
$ git remote get-url origin
../old.git

$ git remote set-url origin ../new.git

$ git remote -v
origin	../new.git (fetch)
origin	../new.git (push)

Both (fetch) and (push) now show the new URL. The name is still origin, so the tracking setting for main still works. After adding one commit and running only git push, the push went to the new URL.

console
$ echo "version: 1.0.0" > app.txt

$ git add app.txt && git commit -q -m "1.0.0 の準備"

$ git push
To ../new.git
   6b9961f..3d7c916  main -> main

$ git ls-remote ../new.git
3d7c9163590ba2e187417493f0142cd7cb580d29	HEAD
3d7c9163590ba2e187417493f0142cd7cb580d29	refs/heads/main

$ git ls-remote ../old.git
6b9961f33795cabbae067b1810a43c9c421b513c	HEAD
6b9961f33795cabbae067b1810a43c9c421b513c	refs/heads/main

The new commit 3d7c916 exists only in ../new.git, and ../old.git is still at 6b9961f. set-url only rewrites the local registration and does nothing to the old location.

Switching a URL from HTTPS to SSH

You can switch between HTTPS and SSH with the same set-url command.

console
$ git remote set-url origin https://example.com/neko/app.git

$ git remote -v
origin	https://example.com/neko/app.git (fetch)
origin	https://example.com/neko/app.git (push)

$ git remote set-url origin git@example.com:neko/app.git

$ git remote -v
origin	git@example.com:neko/app.git (fetch)
origin	git@example.com:neko/app.git (push)

set-url rewrites the URL without checking whether it can connect. When I specified ../nai.git, which does not exist, set-url finished without error, and the error appeared only on the next git fetch.

console
$ git remote set-url origin ../nai.git

$ git fetch origin
fatal: '../nai.git' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

$ git remote set-url origin ../new.git

$ git fetch origin

Running git fetch once after changing the URL is a way to check that the new URL can be fetched from. After I restored the correct URL, git fetch finished without errors.

If you specify a name that is not registered, you get an error.

console
$ git remote set-url upstream ../new.git
error: No such remote 'upstream'

Delete a registration with git remote remove

Before deleting, I registered a second remote named backup and pushed main to it.

console
$ git remote add backup ../backup.git

$ git push -q backup main

$ git remote -v
backup	../backup.git (fetch)
backup	../backup.git (push)
origin	../new.git (fetch)
origin	../new.git (push)

$ git branch -r
  backup/main
  origin/main

I deleted backup with git remote remove.

console
$ git remote remove backup

$ git remote -v
origin	../new.git (fetch)
origin	../new.git (push)

$ git branch -r
  origin/main

$ git config --get-regexp "^remote\."
remote.origin.url ../new.git
remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

The backup/main branch was removed along with the registration. The git remote reference also says that removing a remote deletes all of its remote-tracking branches and configuration.

The remote repository itself is not deleted, though. Looking at ../backup.git directly, the main I pushed is still there.

console
$ git ls-remote ../backup.git
3d7c9163590ba2e187417493f0142cd7cb580d29	HEAD
3d7c9163590ba2e187417493f0142cd7cb580d29	refs/heads/main

If you try to remove the same name again, you get an error.

console
$ git remote remove backup
error: No such remote: 'backup'

Writing rm instead of remove gives the same result.

When you remove a remote that a branch tracks

main tracks origin/main. That setting is stored in the branch's branch.main.remote and branch.main.merge. When I removed origin in this state, the branch-side settings were removed too.

console
$ git config --get-regexp "^branch\."
branch.main.remote origin
branch.main.merge refs/heads/main

$ git remote rm origin

$ git config --get-regexp "^branch\."

$ git branch -vv
* main 3d7c916 1.0.0 の準備

$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

    git remote add <name> <url>

and then push using the remote name

    git push <name>

[origin/main] disappeared from the output of git branch -vv, and git push returned to the same error as before any remote was registered.

Registering the same name again did not restore the tracking setting. At this point origin/main had not been fetched yet, so trying to set it as the upstream with git branch -u failed.

console
$ git remote add origin ../new.git

$ git branch -vv
* main 3d7c916 1.0.0 の準備

$ git config --get-regexp "^branch\."

$ git branch -u origin/main
fatal: the requested upstream branch 'origin/main' does not exist
hint: 
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint: 
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
hint: Disable this message with "git config advice.setUpstreamFailure false"

After I ran git fetch to get origin/main and then set the tracking with git branch -u, the tracking setting was restored (git branch reference).

console
$ git fetch -q origin

$ git branch -u origin/main
branch 'main' set up to track 'origin/main'.

$ git branch -vv
* main 3d7c916 [origin/main] 1.0.0 の準備
Point

If you only want to change the URL, set-url takes less work than removing and re-adding the remote. The name stays the same, so the branch tracking settings are not deleted.

​Posted by 86