git remote

Git remote (Git远程)

Definition

Definition (定义)

The git remote command is designed for creating, viewing and removing connections to other repositories. Remote connections are considered to be bookmarks in other repositories, which are convenient names used for referencing URL that are not convenient enough.

The diagram below displays two remote connections from your repository into the central repository and into the repository of another developer. You can pass the origin to another developer and they will shortcut to other Git commands. As a result, you won’t have to reference the connections by their full URLs. (下图显示了从您的存储库到中央存储库和另一个开发人员的存储库的两个远程连接。您可以将源代码传递给其他开发人员,他们将对其他Git命令进行快捷操作。因此,您不必通过完整的URL来引用连接。)

Creating and editing git remote configurations

Creating and editing git remote configurations

The git remote command is also used for editing a ./.git/config file of the repository. You can do it by executing the commands below:

git remote add

You can get the same result, as in case of executing the commands above, by modifying ./.git/config file with a text director, too.

The origin remote

The origin remote (原点遥控器)

Cloning a repository by executing git clone command, will automatically create a remote connection, that is called origin, and that points back to the cloned repository. Creating a local copy of the central directory is an easy method of pulling upstream modifications or publishing local commits. That’s why, the central repository of many Git-based projects is called “origin”.

Repository URLs

Repository URLs (存储库URL)

There are many ways of referencing a remote repository, but the two simplest ones are considered to be via HTTPS and SSH protocols. In the case of accessing a remote repository via HTTPS, you can’t push commits to an HTTPS address. (有许多方法可以引用远程存储库,但最简单的两种方法被认为是通过HTTPS和SSH协议。 在通过HTTPS访问远程存储库的情况下,不能将提交推送到HTTPS地址。)

http://host/path/to/repo.git

Use SSH for a read and write access. You need a valid SSH account. But take into account, that only authenticated access is supported by Git. Modern secure third party hosting solutions, for example, Bitbucket.com, can provide such URLs:

ssh://user@host/path/to/repo.git

Git remote subcommands

Git remote subcommands (Git远程子命令)

The git remote command has its own subcommands. We are going to examine them below.

The subcommand below will add a record to ./.git/config file for remote named <NAME> at the repository url <URL> . It accepts a -f option, that after the creation of the remote record will instantly git fetch <name>. Another option accepted by this subcommand is a –tags option, which will instantly git fetch <name> and import all the tags from the remote repository.

ADD <NAME> <URL>

The following subcommand will update ./.git/config for renaming the record <OLD> to <NEW>. As a result, all the configuration settings and branches of the remote will be updated.

RENAME <OLD> <NEW>

Next subcommand edits ./.git/config removing the remote named <NAME>. In the end, all the configuration settings and branches for the remote will be removed.

REMOVE or RM

Executing the subcommand below will output the URLs for a remote record. This subcommand accepts –push and –alloptions. (执行下面的子命令将输出远程记录的URL。此子命令接受–push和–alloptions。)

GET-URL <NAME>

The next subcommand will output information about the remote <NAME>.

SHOW <NAME>

And the last command will delete all the local branches for <NAME> that don’t exist on the remote repository. This subcommand accepts –dry-run option which lists the branches that have been set to be pruned, but in fact doesn’t prune them.

PRUNE <NAME>

Git remote examples

Git remote examples (Git远程示例)

While using Git, it’s desirable to be connected to other developers’ repositories too, because this gives the opportunity to collaborate outside of the central repository and have effective work. For example, your co-worker has kept a repository dev.example.com/tom.git that is publicly accessible, you can add a connection like the following:

git remote add tom http://dev.example.com/tom.git

Showing your remotes

Showing your remotes (显示您的遥控器)

By default, the git remote command lists all the remote connections that have previously been stored to other repos. As a result, you will have a single line output listing the names of bookmarked remote repositories.

git remote
origin
upstream
other_repo

You can invoke the git remote command with the -v option that will list bookmarked repository names with the URL of the corresponding repository.

git remote -v
origin  [email protected]:origin_user/reponame.git (fetch)
origin  [email protected]:origin_user/reponame.git (push)
upstream    https://example.com/upstream_user/reponame.git (fetch)
upstream    https://example.com/upstream_user/reponame.git (push)
other_repo    https://example.com/other_repo/reponame (fetch)
other_repo    https://example.com/other_repo/r

Adding remote repositories

Adding remote repositories (添加远程存储库)

Invoking the git remote add command, a new connection record will be created to a remote repository. Then the <name> name will be available as an acceptable shortcut for <url> in other Git commands. This command creates a new record in the ./.git/config of the repository.

git remote add fake_test https://example.com/upstream_user/reponame.git; [remote "remote_test"] 
url = https://example.com/upstream_user/reponame.git 
fetch = +refs/heads/*:refs/remotes/remote_test/*

Inspecting a remote

Inspecting a remote (检查遥控器)

You can also attach the show subcommand to the git remote command to have more detailed output on the configuration of a remote, included a list of those branches that are connected with the remote and the endpoints appended for fetching and pushing.

git remote show upstream
* remote upstream
Fetch URL: https://example.com/upstream_user/reponame.git
Push URL: https://example.com/upstream_user/reponame.git
HEAD branch: master
Remote branches:
master tracked
simd-deprecated tracked
(simd-deprecated跟踪)
tutorial tracked
Local ref configured for 'git push':
master pushes to master (fast-forwardable)

Fetching and pulling from Git remotes

Fetching and pulling from Git remotes (从Git遥控器获取和拉取)

Immediately after the configuration of a remote record done with the git remote command, the remote name can serve as an argument for other Git commands to create a communication with the remote repository. You can use git fetch and git pull commands for reading from a remote repository.

Pushing to Git remotes

Pushing to Git remotes (推送到Git遥控器)

The git push command writes to a remote repository.

git push <remote-name> <branch-name>

Renaming and removing remotes

Renaming and removing remotes (重命名和删除遥控器)

The git remote command renames a remote connection from <that-name> to <this-name>. Besides, it modifies the contents of ./.git/config to rename the record for the remote as well.

git remote rename <that-name> <this-name>

The git remote rm command removes the connection to the remote repository defined by the <name> parameter.

git remote rm <name>


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部