【问题标题】:Visual Studio Code - Connect to a remote Git repository and PUSH local files to a new remote repositoryVisual Studio Code - 连接到远程 Git 存储库并将本地文件推送到新的远程存储库
【发布时间】:2017-09-07 22:06:36
【问题描述】:

我使用 Visual Studio Code 创建了一个本地项目,它实现了一个本地 Git 存储库

然后我在 Visual Studio Online 上创建了一个 Git 存储库,我想将我的所有项目文件推送到远程存储库...

正确的做法是什么?

此时我的 .git\config 文件如下所示:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true

【问题讨论】:

  • VSCode 在其 UI 中不支持“添加远程”操作。所以,我也必须像@Christoph 建议的那样手动完成
  • 我看了又看,然后我找到了这篇文章。如果您已经在 VS Code 中设置了您的解决方案,然后创建了您的 GitHub 存储库并希望将您的代码推送到新的存储库,那么这个答案就可以了。

标签: git visual-studio-code


【解决方案1】:

我假设您开始在一个目录中工作并且没有在那里使用 Git。以下内容应适用于Git Bash

cd "path to your repository"
git init
git add . # If you want to commit everything. Otherwise use .gitconfig files
git commit -m "initial commit" # If you change anything, you can add and commit again...

要添加遥控器,只需这样做

git remote add origin https://...
git remote show origin # If everything is ok, you will see your remote
git push -u origin master # Assuming you are on the master branch.

-u 设置上游引用,Git 知道将来从哪里到 fetch/pull 以及从哪里到 push

【讨论】:

  • 感谢 Christoph,我将是唯一可以访问存储库的人,而且我肯定不需要创建分支。第一行那个“原点”,说明了什么?
  • 只是整个地址的缩写。你可以给它起任何名字,但如果他们只有一个远程地址,大多数人称之为origin。你应该阅读this ;-)
  • 谢谢,还有一个简单的问题:定义本地存储库、复制文件然后运行您提供的命令以在远程存储库上发布文件是否正确?还是有更好的方法来做到这一点?
  • 你不需要复制任何东西!你为什么要这么做?
  • 我强烈建议您通读this。如果还有不清楚的地方,只需在 SO 上写一个问题 ;-) 开始另一个教程是没有意义的。
【解决方案2】:

现在可以在不输入命令行的情况下从 Visual Studio Code 设置原点:

git remote add origin https://github.com/USR/REPO.git

如果您在 Windows 上,则可以在 Visual Studio Code 中 Ctrl + SHIFT + P 并键入 remote

从那里选择Git: Add Remote,您将有两个步骤:

【讨论】:

  • 我正在寻找应该给出的“远程名称”。感谢您明确提及。
【解决方案3】:

From comments:

VSCode 在其 UI 中不支持“添加远程”操作(2018 年 4 月)

实际上,从Visual Studio Code 1.46 (May 2020):

从 GitHub 添加远程

您现在可以使用 Git: Add Remote... 命令轻松地将 GitHub 存储库作为远程添加到本地存储库。

【讨论】:

  • 它在我的 git 项目上完成自动完成后,我选择了它并按回车键。然后它再次提示我使用空白编辑框。没有消息。只是不工作。
猜你喜欢
  • 1970-01-01
  • 2020-11-27
  • 2015-04-14
  • 1970-01-01
  • 2018-06-24
  • 2010-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多