git remote command

Git Basic Operations


git remoteThis command is used to manage remote repositories in a Git repository.

git remoteThe command provides functions for viewing, adding, renaming, and deleting remote repositories.

The following are common usages of the git remote command:

  • git remote: List the configured remote repositories in the current repository.
  • git remote -v: List the configured remote repositories in the current repository and display their URLs.
  • git remote add <remote_name> <remote_url>: Add a new remote repository. Specify a name and URL for a remote repository to add it to the current repository.
  • git remote rename <old_name> <new_name>Rename the configured remote repository.
  • git remote remove <remote_name>: Remove the specified remote repository from the current repository.
  • git remote set-url <remote_name> <new_url>: Modify the URL of the specified remote repository.
  • git remote show <remote_name>: Display detailed information about the specified remote repository, including its URL and tracking branches.

The following lists the usage for viewing remote repositories, adding a remote repository, renaming a remote repository, deleting a remote repository, modifying a remote repository URL, and viewing remote repository information:

git remote
git remote -v
git remote add origin https://github.com/user/repo.git
git remote rename origin new-origin
git remote remove new-origin
git remote set-url origin https://github.com/user/new-repo.git
git remote show origin

Application example

In this chapter, we will use Github as the remote repository for operations, so before reading this chapter, you need to first read the related content about Github:Git Remote Repository (Github)。

Display all remote repositories:

git remote -v

git remote -vYou can view the list of remote repositories configured in the current repository and their URLs.

Below, we first load the remote repository, and then view the information:

$ git clone https://github.com/tianqixin/example-git-test
$ cd example-git-test
$ git remote -v
origin  https://github.com/tianqixin/example-git-test (fetch)
origin  https://github.com/tianqixin/example-git-test (push)

originIt is an alias for the remote address.

Display information about a specific remote repository:

git remote show [remote]

For example:

$ git remote show https://github.com/tianqixin/example-git-test
* remote https://github.com/tianqixin/example-git-test
  Fetch URL: https://github.com/tianqixin/example-git-test
  Push  URL: https://github.com/tianqixin/example-git-test
  HEAD branch: master
  Local ref configured for 'git push':
    master pushes to master (local out of date)

Add a remote repository:

git remote add <remote_name> <remote_url>
  • <remote_name>: The name of the remote repository to add. Usually, the name of the remote repository isorigin, but you can also customize a name.
  • <remote_url>: The URL of the remote repository. It can be an HTTPS, SSH, or Git protocol link pointing to a remote Git repository.

The following command will add a remote repository named origin to the current Git repository, with its URL being https://github.com/user/repo.git.

git remote add origin https://github.com/user/repo.git

Example usage:

# 提交到 Github
$ git remote add origin [email protected]:tianqixin/example-git-test.git
$ git push -u origin master

After adding a remote repository, you can use other Git commands to interact with the remote repository, such as pushing local code to the remote repository, pulling code from the remote repository, and so on.

Other related commands:

git remote rm name  # 删除远程仓库
git remote rename old_name new_name  # 修改仓库名

For more content, see:Git Remote Repository (Github)。



Git Basic Operations

other extensions