How to Set Up Git Remote

Stewart Nguyen Feb 02, 2024
How to Set Up Git Remote

This article will introduce how to add or remove remote repositories.

Remote repositories are projects hosted somewhere, such as Github/Gitlab.

Adding remote repositories allows us to use a short name (alias) to execute commands instead of typing a whole remote URL.

$ git fetch git@github.com:stwarts/git-demo.git
From github.com:stwarts/git-demo
 * branch            HEAD       -> FETCH_HEAD

A default remote name called origin is set up automatically when using git clone.

Its value is the cloned repository.

$ git clone git@github.com:stwarts/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
$ cd git-demo
$ git remote -v
origin git@github.com:stwarts/git-demo.git (fetch)
origin git@github.com:stwarts/git-demo.git (push)

Use git remote -v to list all remotes details; to display just the names, use git remote.

  • -v option stands for --verbose, which shows the corresponding URL to each name.
$ git remote
origin
$ git remote -v
origin git@github.com:stwarts/git-demo.git (fetch)
origin git@github.com:stwarts/git-demo.git (push)

When initializing a repository in local, we need to add remote explicitly.

git remote add <name> <url>
$ git remote add alice_git_demo git@github.com:alice/git-demo.git
$ git remote -v
alice_git_demo git@github.com:alice/git-demo.git (fetch)
alice_git_demo git@github.com:alice/git-demo.git (push)
origin git@github.com:stwarts/git-demo.git (fetch)
origin git@github.com:stwarts/git-demo.git (push)

We can run git remote rename <old> <new> to rename an existing remote.

$ git remote rename alice_git_demo stwarts_git_demo
origin git@github.com:stwarts/git-demo.git (fetch)
origin git@github.com:stwarts/git-demo.git (push)
stwarts_git_demo git@github.com:alice/git-demo.git (fetch)
stwarts_git_demo git@github.com:alice/git-demo.git (push)

Use git remote remove <name> to remove a tracked repository.

$ git remote remove stwarts_git_demo
$ git remote remove origin
$ git remote add stwarts_git_demo git@github.com:stwarts/git-demo.git
$ git remote -v
stwarts_git_demo git@github.com:stwarts/git-demo.git (fetch)
stwarts_git_demo git@github.com:stwarts/git-demo.git (push)

stwarts_git_demo name is ready to be used for sharing data.

$ git fetch stwarts_git_demo
From github.com:stwarts/git-demo
 * [new branch]      main       -> stwarts_git_demo/main
$ touch file.txt
$ git add file.txt && git commit -m 'Add file.txt'
[main 80bf058] Add file.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file.txt
$ git push stwarts_git_demo main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 93.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:stwarts/git-demo.git
   dd38fe8..80bf058  main -> main

Related Article - Git Remote