Detailed explanation of git remote command

1. Use scenario
2. Push code pipeline
3. Add remote library configuration
4. Modify remote library configuration 5. Delete remote library configuration 6. Rename remote library configuration
7. Push to multiple warehouses 8. View remote library configuration 9. View Remote library information and the relationship with the local library



1. Usage scenarios


After reading this article, you can solve the following problems:

1. When the local code needs to be uploaded to the remote warehouse.
2. When the local warehouse already exists,
and the local warehouse needs to be pushed to the remote warehouse. 3. When the local warehouse already exists and is associated with the remote warehouse, the associated remote warehouse needs to be changed.

2. Push the code pipeline


 
 
  1. # 第一步: 创建本地库并完成初始提交,也就是让本地库有提交记录
  2. git init
  3. git add .
  4. git commit -m "first commit"
  5. # 第二步: 添加一个远程仓库配置
  6. git remote add origin https://gitee.com/holyking/test-1.git
  7. # 第三步: 设置上游分支,并且使用远程名称推送到远程库
  8. git push -u origin master

3. Add remote library configuration


The following prompt appears when the code is pushed to the remote warehouse for the first time:

 

 
 
  1. # 没有配置推送目标
  2. fatal: No configured push destination.
  3. # 从命令行指定 URL,或使用配置远程存储库
  4. Either specify the URL from the command-line or configure a remote repository using
  5. # 然后使用远程名称推送
  6. and then push using the remote name

Specifying URLs from the command line

 
 
  1. # 命令格式
  2. git push <url> <branch>
  3. # 使用示例
  4. git push [email protected]:holyking/test-1.git master

First configure a remote repository, and then use the remote name to push (in fact, give the remote library url a relatively short name, and then use the short name to push)

 
 
  1. # 命令格式
  2. git remote add <name> <url>
  3. git push <name> <branch>
  4. # 使用示例
  5. git remote add origin [email protected]:holyking/test-1.git
  6. git push origin master

4. Modify the remote library configuration


If the local warehouse has been associated with the remote warehouse,  git remote add an error will be reported when using the direct link to the new remote warehouse

 
 
  1. fatal: remote origin already exists.

 

Solution 1: First delete the previous remote warehouse association, and then associate the new remote warehouse

 
 
  1. git remote rm origin
  2. git remote add origin https://gitee.com/holyking/test-3.git

 

Solution 2: Use git remote set-url origin to directly modify the associated remote warehouse

 
 
  1. # 使用前提: 远程名称 origin 已存在
  2. git remote set-url origin https://gitee.com/holyking/test-3.git

Modify the associated remote warehouse summary:

 
 
  1. # 方案1: 先删除远程库配置,再重新添加
  2. git remote rm <name>
  3. git remote add <name> <url>
  4. # 方案2: 使用 set-url 直接修改
  5. git remote set-url <name> <url>

5. Delete the remote library configuration


Delete remote library configuration

 
 
  1. # 命令格式
  2. git remote remove <name>
  3. # 使用示例
  4. git remote remove origin

After testing  rm, remove the effect is the same, so you can also use the following usage

 
 
  1. git remote rm <name>

6. Rename the remote repository configuration


 
 
  1. # 命令格式
  2. git remote rename <old> <new>
  3. # 使用示例
  4. git remote rename origin liang

7. Push to multiple warehouses


Add remote library configuration (we want to push the code to gitee and github platforms)

 
 
  1. # gitee
  2. git remote add gitee [email protected]:holyking/test-1.git
  3. # github
  4. git remote add github [email protected]:shinyboys/test-2.git

Push the code to gitee and github

 
 
  1. git push gitee master && git push github master

When pushing to the remote library, because the command is a bit long, we can define a system configuration alias to simplify the push command

 
 
  1. # mac 用户可以在 ~/.zshrc 文件中增加以下配置,通过别名 gp 推送
  2. alias gp="git push gitee master && git push github master"

8. View remote library configuration


When there is no parameter, it is to list the existing remote branches

 
 
  1. git remote

-v,--verbose View all remote warehouse configurations

 
 
  1. git remote -v

9. View the remote library information and the relationship with the local library


This command will query the remote library information online, and will list the relationship with the local library

 
 
  1. # 命令格式
  2. git remote show <name>
  3. # 使用示例
  4. git remote show origin

Guess you like

Origin blog.csdn.net/linyichao123/article/details/128475299