【问题标题】:Push a git repo to Gitlab not using the global settings不使用全局设置将 git repo 推送到 Gitlab
【发布时间】:2021-03-14 10:07:38
【问题描述】:

我正在尝试将 git repo 推送到 Gitlab。

我有用于工作的 --global git 和 ssh 设置。我遇到问题的一个仓库是一个个人项目,所以我想对我的全局工作配置使用不同的 user.nameuser.email git 配置设置和不同的 ssh 密钥对。

我在 git repo 中设置了 user.nameuser.email(没有 --global 开关),使用:

git config user.name "my gitlab username"
git config user.email "my gitlab registered email address"

我已将 git config 设置为使用本地 ssh 密钥文件,而不是使用系统文件:

git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/path-to-ed25519 -F /dev/null"

在 gitlab 上,我创建了一个与本地 repo 同名的 repo,并添加了 ssh 公钥。

Gitlab 推荐这些命令来推送项目:

cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.com:project.git
git push -u origin --all
git push -u origin --tags

git remote add origin git@gitlab.com:project.git 命令似乎成功了,至少没有错误。 git push -u origin --all 命令失败,输出如下:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: the remote end hung up unexpectedly
Everything up-to-date

Gitlab 建议使用 ssh -T git@gitlab.example.com 测试 ssh 设置,但我不清楚如何从 repo url 填充 example.com。我已经尝试了所有明显的组合,但没有任何乐趣。我还尝试了 -i 选项来指定要使用的私钥和 -Tvvv 选项而不是 -T 选项,但不清楚这是否有效。输出如下:

OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "gitlab.com:username/project" port 22
ssh: Could not resolve hostname gitlab.com:username/project: Name or service not known

任何和所有帮助表示赞赏...

【问题讨论】:

  • 问题标题应该说“不使用”而不是“不使用”。似乎无法编辑我自己的问题。如果有模组阅读,请修复并删除此评论。
  • 为什么不对两个 repo 使用相同的密钥?其他解决方案,将 ssh 用于您的公共存储库并使用 http 凭证作为特定存储库。
  • 我觉得http现在贬值了?至少 Pycharm 一直在唠叨我不要再使用它了。
  • user.nameuser.email 不用于身份验证(使用 ssh https)。 (其余评论已删除,我明白我现在误解了什么)
  • 最后的ssh失败是因为你跑了ssh -T git@gitlab.com:username/project;你需要ssh -T git@gitlab.com,除非你的core.sshCommand 设置受到尊重,你需要 ssh -T -o IdentitiesOnly=yes -i ...`(基本上是你在这里设置的所有选项)。

标签: git ssh gitlab


【解决方案1】:

实际的网址是:

 git remote add origin git@gitlab.com:username/project.git
 # not: 
 git remote add origin git@gitlab.com:project.git

仔细检查 GitLab 上的远程存储库是否使用默认分支 main 创建,而您的本地分支可能是 master
如果是这种情况,请尝试:

git push -u origin master:main

自 2021 年 3 月 10 日起,GitLab can use main for new repositories

每个 Git 存储库都有一个初始分支,这是生成新存储库时要创建的第一个分支。从历史上看,这个初始分支的默认名称是master
这个术语来自 Git 的前身 Bitkeeper。 Bitkeeper 将真实来源称为“主存储库”,将其他副本称为“从存储库”。
这显示了技术中主/从引用的常见程度,以及理解如何解释术语主的困难。

Git 项目维护人员与更广泛的社区合作,一直在听取开发社区的反馈,为默认分支确定更具描述性和包容性的名称,并为用户提供更改默认分支名称的选项(通常master) 的存储库。

【讨论】:

  • 这修复了它:git push -u origin master:mainty
猜你喜欢
  • 2018-04-25
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
相关资源
最近更新 更多