VS Code如何在现有的本地项目中使用github

问题描述 投票:4回答:5

我在C:\ ABC文件夹中有一个本地项目,只是意识到是时候将它备份到GitHub了。这就是我做的:

  1. 登录GitHub,创建一个新项目(存储库或任何你可以调用的)。
  2. 复制URL。
  3. 返回VS Code,安装GitHub扩展。
  4. 转到欢迎页面,单击“克隆Git存储库...”。粘贴“存储库URL”的URL。输入我的本地路径C:\ ABC ,.

错误Git:致命:目标路径“C:\ ABC”已存在且不是空目录。

我知道它试图从GitHub获取到本地,但我想要的是相反的。谷歌搜索发现了许多关于如何从头开始的文章和视频,但没有找到现有的本地项目。

github
5个回答
4
投票

尝试使用命令行:

cd c:\ABC
git init .
git config --global user.name <yourGitHubAccount>
git config --global user.email <yourGitHubEmailAccount>
git add .

git status
# edit .gitignore to ignore folder you don't want

git commit -m "first commit"
git remote add origin https://github.com/<yourGitHubAccount>/<yourRepo.git>
git push -u origin master

确保你的GitHub仓库实际上是空的(没有README.md


2
投票

@VonC,重新做了同样的步骤:1)创建一个新的本地空目录,在VS Code链接本地空目录到GitHub的空存储库,如上所述。成功了。

2)用我的代码文件/目录填充这个本地空目录。

3)git init . system msg:重新初始化现有的Git存储库... / .git /

3)一次运行这两个命令,没有错误也没有消息。

git config --global user.name Jeb50 
git config --global user.email [email protected]

4)git add .系统消息:

warning: LF will be replaced by CRLF in .vscode/launch.json.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in Angular/Modules/mainApp.js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in content/Site.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in package-lock.json.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in package.json.
The file will have its original line endings in your working directory.

5)git status系统消息:

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   .vscode/launch.json
        new file:   Angular/Controllers/AuthController.js
        new file:   Angular/Controllers/MainController.js
        new file:   Angular/Models/user.js
        new file:   Angular/Modules/mainApp.js
        new file:   Passport/passport-init.js
        new file:   Routes/authentication.js
        new file:   Routes/routerMEAN2.js
        new file:   Untitled.png
        new file:   Views/Authentication/login.html
        new file:   Views/Authentication/register.html
        new file:   Views/Authentication/unauth.html
        new file:   Views/Main/About.html
        new file:   Views/Main/Contact.html
        new file:   Views/Main/index.html
        new file:   Views/Starter.ejs
        new file:   content/Site.css
        new file:   package-lock.json
        new file:   package.json
        new file:   server.js

6)我的.gitignore文件:

# individual files
9222.jpg
err1.jpg
config.js

# exclude  folders
node_modules/

7)git commit -m "first commit",似乎没问题但是没有在存储库系统msg下显示的代码:

[master (root-commit) f8e435e] first commit
 21 files changed, 2305 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 .vscode/launch.json
 create mode 100644 Angular/Controllers/AuthController.js
 create mode 100644 Angular/Controllers/MainController.js
 create mode 100644 Angular/Models/user.js
 create mode 100644 Angular/Modules/mainApp.js
 create mode 100644 Passport/passport-init.js
 create mode 100644 Routes/authentication.js
 create mode 100644 Routes/routerMEAN2.js
 create mode 100644 Untitled.png
 create mode 100644 Views/Authentication/login.html
 create mode 100644 Views/Authentication/register.html
 create mode 100644 Views/Authentication/unauth.html
 create mode 100644 Views/Main/About.html
 create mode 100644 Views/Main/Contact.html
 create mode 100644 Views/Main/index.html
 create mode 100644 Views/Starter.ejs
 create mode 100644 content/Site.css
 create mode 100644 package-lock.json
 create mode 100644 package.json
 create mode 100644 server.js

8)git remote add origin https://github.com/Jeb50/MEAN2.git系统消息:

fatal: remote origin already exists.

9)git push -u origin master系统消息:

Counting objects: 34, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (34/34), 30.77 KiB | 0 bytes/s, done.
Total 34 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/TLKG/MEAN2.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

在GitHub刷新代码选项卡,显示预期的文件。似乎工作。

真棒!


2
投票

我只能通过三次点击使用github桌面应用程序来执行此操作:https://help.github.com/en/desktop/contributing-to-projects/adding-a-repository-from-your-local-computer-to-github-desktop

关键方向是:您可以将任何Git存储库添加到GitHub Desktop,即使它不是GitHub存储库。

它将识别该目录不是repo并询问您是否要创建一个目录。 screenshot of what to click to create repo

点击创建仓库后,您将看到以下内容:

full repo create dialogue

最后,它甚至会提示您将其发布到Github进行备份:

publish to github


0
投票

这是我使用SourceTree将VSCode中的现有文件夹/项目添加到GitHub的解决方案:

  1. 在GitHub上创建一个repo。创建后,您可以创建.gitignore或在步骤3中手动执行此操作。
  2. 从命令行项目的根目录键入以下内容,将此项目初始化为Git仓库:git init
  3. 在根目录中手动为您的特定项目创建.gitignore(如果尚未在步骤1的repo中创建),请确保忽略所有相关文件(即Web项目的node_modules)。最好从Github中的现有模板复制完成的(https://github.com/github/gitignore
  4. 打开SourceTree并按“克隆/新建”
  5. 选择“添加工作副本标签”
  6. 在“工作副本路径”中,使用省略号找到在步骤#1中初始化Git项目的根目录。你知道你做得对,因为在对话框的“存储库类型”旁边,你应该看到一条消息,指出“这是一个Git存储库”。关闭对话框。
  7. 转到右上角的“设置”和“添加”遥控器。
  8. 假设默认设置,'Name'为'origin'(选择'Default remote'),对于URL / Path,输入在步骤1中创建repo时提供的完整GitHub路径。按OK。 (即https://github.com/MyUserName/MyCoolCode.git
  9. 暂存并提交您的文件
  10. 将主分支推送到“原点”

我承认,我认为GitHub提供的初始化repo并执行第一次提交的命令行指令更容易。但是,此时你的GitHub仓库在SourceTree中都正确连接,你很高兴。

我还要说,如果你可以提前考虑,在做任何工作之前创建一个空的仓库并在SourceTree中克隆它要容易得多。从第1天开始,这肯定是最容易跟踪的。但是,上述步骤适用于添加现有解决方案。


0
投票

这个解决方案可能很蹩脚但对我来说就像一个魅力。

1)登录GitHub,创建一个与项目类似的存储库。 2)复制URL。 3)打开VS Code将存储库克隆到本地使用。 这可以通过按下命令+ p来完成,这将打开一个输入框。 在输入框中编写克隆并选择克隆。 粘贴git存储库URL并选择要克隆的任何空目录。 4)打开本地项目,将除.git文件夹之外的所有内容复制并粘贴到克隆的存储库中。 5)现在在VS Code中打开新的克隆项目。 6)转到视图> SCM 7)在输入中输入一些注释(类似于初始提交)。 8)单击检查图标,选择是并输入一些消息。 9)点击SCM选项(检查按钮旁边的三个点)并发布分支。

© www.soinside.com 2019 - 2024. All rights reserved.