介绍:
Remote 可以有几个,每一个通常是只读、只写或读/写,它可以理解为是远端代码托管平台的地址。Git 借助 remote 可以实现添加远程存储库、删除不再有效的远程存储库、管理各种远程分支并定义它们是否被跟踪等等。
仓库 remote 用于指向本地仓库在 codechina 托管的地址,目前比较常见的有 ssh 格式的,如:
git@gitlab.youkuaiyun.com:code-china/gitlab/codechina-workflow.git
http 格式,如:
https://gitlab.com/lsh0310/codechina-workflow.git
其中 http 格式中除上面案例中以外,codechina 还支持用户名密码格式以及 auth2 + access token 格式,如:
// 用户名密码
https://csdn-username:csdn-password@codechina.youkuaiyun.com/lsh0310/codechina-workflow.git
// access token
https://oauth2:personal-accesstoken@codechina.youkuaiyun.com/lsh0310/codechina-workflow.git
注意:access token 可以在 codechina 的用户设置-访问令牌 中添加,必须有 API 以上访问权限才可以推送仓库代码。
在实际使用上,推荐采用 ssh remote ,其后续代码操作更为简单。
remote 管理:
新增一个 remote: git remote add <shortname> <url>
example:
git remote add origin1 https://codechina.youkuaiyun.com/lsh0310/codechina-workflow.git
删除一个 remote:git remote rm <shortname>
example:
git remote rm origin1
查看当前仓库的 remote:git remote -v
example:
origin1 https://codechina.youkuaiyun.com/lsh0310/codechina-workflow.git (fetch)
origin1 https://codechina.youkuaiyun.com/lsh0310/codechina-workflow.git (push)
origin2 git@gitlab.youkuaiyun.com:code-china/gitlab/codechina-workflow.git (fetch)
origin2 git@gitlab.youkuaiyun.com:code-china/gitlab/codechina-workflow.git (push)
origin3 https://oauth2:AGgaZ9SrZ2Qb8bNqJnUM@codechina.youkuaiyun.com/lsh0310/codechina-workflow.git (fetch)
origin3 https://oauth2:AGgaZ9SrZ2Qb8bNqJnUM@codechina.youkuaiyun.com/lsh0310/codechina-workflow.git (push)
origin4 https://lish:a03103498356@codechina.youkuaiyun.com/lsh0310/codechina-workflow.git (fetch)
origin4 https://lish:a03103498356@codechina.youkuaiyun.com/lsh0310/codechina-workflow.git (push)
异常处理:
向 codechina 平台推送代码时,提示“‘origin’ does not appear to be a git repository”时,有可能是仓库在 codechina 平台没有初始化,这时首先要确保仓库 remote 已经正确设置,然后使用 git push -u origin master
命令进行关联和代码推送。