【问题标题】:Creating git repo and pushing local directory to freshly initialized Azure Repos创建 git repo 并将本地目录推送到新初始化的 Azure Repos
【发布时间】:2020-04-21 09:31:44
【问题描述】:

我创建了本地 git repo。 (git init、git add、git commit)。然后我在 Azure DevOps 上创建了 git repo。 我也在本地执行git remote add origin <azure_repo_url>

现在当我尝试git push origin master 时,我回来了:

To azure_repo_url
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'azure_repo_url'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

所以我先尝试了pull:git pull origin master,但又回来了:

$ git pull origin master
warning: no common commits
remote: Azure Repos
remote: We noticed you're using an older version of Git. For the best experience, upgrade to a newer version.
remote: Found 3 objects to send. (27 ms)
Unpacking objects: 100% (3/3), done.
From azure_repo_url
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

我可以通过git pull origin master --allow-unrelated-histories从这里so解决它, 但我想知道是否有更清洁的方法可以做到这一点?

【问题讨论】:

  • 有很多选择。你可以变基。你可以强行推动。是否要保留您创建的存储库中的历史记录?
  • @DanielMann,在 github 中有选项 ...or push an existing repository from the command line 我想在 Azure 上做同样的事情,即从我的本地 repo 填充 Azure repo。
  • 然后强制推送。
  • 从你的观点来看,你现在只有两条路可走——要么是git pull,要么是git push --force。两者都在链接的问题中进行了讨论。
  • 如果您在 Azure 存储库中还没有历史记录,那么只需强制推送。如果你这样做,我的建议是克隆 Azure 存储库并将新的本地文件作为新提交移动到该存储库中,然后推送。

标签: git azure-devops azure-repos


【解决方案1】:

在 Azure DevOps 上创建新存储库时,您可以选择对其进行初始化,也可以将其保留。初始化存储库时,Azure DevOps 会添加 .gitIgnore 和 readme.md。

这通常是您看到的推/拉问题的原因。

解决它的最简单方法是:

git remote add origin <azure_repo_url>
git fetch origin
git rebase master --onto origin/master
... fix any merge conflicts ...
git push origin master

或:

git remote add origin <azure_repo_url>
git pull --rebase
... fix any merge conflicts ...
git push origin master

最终结果是您最终得到了本地更改和生成的忽略和自述文件。如果遥控器上有更多更改,您可能需要执行更高级的合并版本,但对于其他干净的遥控器,应该这样做。

【讨论】:

  • 太棒了,取消点击add readme 就行了,我给了我Push an existing repository from command line 的选项。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-04-25
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2022-11-30
  • 2015-10-25
  • 2014-11-01
相关资源
最近更新 更多