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
# 第一步: 创建本地库并完成初始提交,也就是让本地库有提交记录
git init
git add .
git commit -m "first commit"
# 第二步: 添加一个远程仓库配置
git remote add origin https://gitee.com/holyking/test-1.git
# 第三步: 设置上游分支,并且使用远程名称推送到远程库
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:
# 没有配置推送目标
fatal: No configured push destination.
# 从命令行指定 URL,或使用配置远程存储库
Either specify the URL from the command-line or configure a remote repository using
# 然后使用远程名称推送
and then push using the remote name
Specifying URLs from the command line
# 命令格式
git push <url> <branch>
# 使用示例
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)
# 命令格式
git remote add <name> <url>
git push <name> <branch>
# 使用示例
git remote add origin [email protected]:holyking/test-1.git
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
fatal: remote origin already exists.
Solution 1: First delete the previous remote warehouse association, and then associate the new remote warehouse
git remote rm origin
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
# 使用前提: 远程名称 origin 已存在
git remote set-url origin https://gitee.com/holyking/test-3.git
Modify the associated remote warehouse summary:
# 方案1: 先删除远程库配置,再重新添加
git remote rm <name>
git remote add <name> <url>
# 方案2: 使用 set-url 直接修改
git remote set-url <name> <url>
5. Delete the remote library configuration
Delete remote library configuration
# 命令格式
git remote remove <name>
# 使用示例
git remote remove origin
After testing rm
, remove
the effect is the same, so you can also use the following usage
git remote rm <name>
6. Rename the remote repository configuration
# 命令格式
git remote rename <old> <new>
# 使用示例
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)
# gitee
git remote add gitee [email protected]:holyking/test-1.git
# github
git remote add github [email protected]:shinyboys/test-2.git
Push the code to gitee and github
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
# mac 用户可以在 ~/.zshrc 文件中增加以下配置,通过别名 gp 推送
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
git remote
-v,--verbose
View all remote warehouse configurations
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
# 命令格式
git remote show <name>
# 使用示例
git remote show origin