将当前代码推送到现有 GitHub 存储库

Push current code to existing GitHub repository

我有几个 Visual Studio 解决方案,它们都有一个本地存储库和一个在 GitHub 上。我已经做了很多更改并成功将这些更改推送到 GitHub。

但现在 Visual Studio 忘记了我的一个本地存储库与 GitHub 存储库相关联,我似乎无法弄清楚如何重新连接它。事实上,它不再在我的 GitHub 个存储库列表中列出该存储库。

在下图中,您可以看到我有一个名为 Toxic 的本地存储库,但该存储库未出现在 GitHub 个存储库列表中。如果我尝试将 Toxic 项目发布到 GitHub,它只会告诉我存储库已经存在。

我怎么才能让所有现有的 Github 存储库显示在上面显示的顶部部分,以便我可以推送我的最新更改?

除非我遗漏了什么,看来唯一的选择是在本地克隆 GitHub 存储库,将我修改的文件复制到新创建的存储库中,然后签入我的更改。

当然,我丢失了自上次登录 GitHub 以来的所有评论和迭代。并且必须注意不要删除 .git 文件夹,并复制所有更改的文件并删除任何已删除的文件。似乎应该有更简单的方法,但这确实成功了。

it appears the only option is to clone the GitHub repository locally, copy my modified files over the newly created repository, and then check in my changes.

试拳:

  • 正在安装 Git for Windows (command-line)
  • 在新文件夹中克隆您的远程仓库
  • 将现有存储库添加为远程
  • 获取并查看是否可以cherry-pick你的提交

即:

 git clone https://github.com/<user>/<repo> newFolder
 cd newFolder
 git remote add old ../path/to/old/local/repo
 git fetch old
 git log old/master
 git cherry-pick old-sha1..old/master

(old-sha1 是你想要返回的第一个提交)

然后在您的 Visual Studio 工作区中添加新文件夹,并查看是否一切正常(进行修改、添加、提交和推送)

我不是 git 专家,但我想我可能会提供帮助,如果我还不算太晚的话。

运行:

git remote -v

这应该以以下形式打印内容:

origin  <remote_repo_url> (fetch)
origin  <remote_repo_url> (push)

如果你只看到:

origin  (fetch)
origin  (push)

尝试运行宁:

git remote set-url origin <remote_repo_url>

如果没有输出那么运行:

git remote add origin <remote_repo_url>

然后尝试

git push -u origin

-u--set-upstream 标志会将 origin 设置为分支的默认回购。