git 如何使用 vs 代码将新的初始项目推送到 github

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46877667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:09:17  来源:igfitidea点击:

How to push a new initial project to github using vs code

gitgithubvisual-studio-codevscode-settings

提问by Xsmael

All the tutorials i've seen till now shows to first create a repository on github, copy the link go to vscode and git cloneit and from that on, you can do commits and pushes.

到目前为止,我看到的所有教程都表明首先在 github 上创建一个存储库,将链接复制到 vscode,git clone然后从那里开始,您可以进行提交和推送。

Is that the right way ? can't I just start a project from vscode and then upload it to my git ?

那是正确的方法吗?我不能从 vscode 开始一个项目,然后将它上传到我的 git 吗?

回答by Vikram K

Here are the detailed steps needed to achieve this.

以下是实现这一目标所需的详细步骤。

The existing commands can be simply run via the CLI terminal of VS-CODE. It is understood that Git is installed in the system, configured with desired username and email Id.

现有的命令可以通过 VS-CODE 的 CLI 终端简单地运行。据了解,系统中安装了Git,配置了想要的用户名和邮箱ID。

1) Navigate to the local project directory and create a local git repository:

1)导航到本地项目目录并创建本地git存储库:

 git init

2) Once that is successful, click on the 'Source Control' icon on the left navbar in VS-Code.One should be able to see files ready to be commit-ed. Press on 'Commit' button, provide comments, stage the changes and commit the files. Alternatively you can run from CLI

2) 一旦成功,点击 VS-Code 左侧导航栏上的“源代码控制”图标。人们应该能够看到准备提交的文件。按“提交”按钮,提供评论,暂存更改并提交文件。或者,您可以从 CLI 运行

git commit -m "Your comment"

3) Now you need to visit your GitHub account and create a new Repository. Exclude creating 'README.md', '.gitIgnore' files. Also do not add any License to the repo. Sometimes these settings cause issue while pushing in.

3) 现在您需要访问您的 GitHub 帐户并创建一个新的存储库。排除创建“README.md”、“.gitIgnore”文件。也不要向 repo 添加任何许可证。有时这些设置会在推入时导致问题。

4) Copy the link to this newly created GitHub Repository.

4) 将链接复制到这个新创建的 GitHub 存储库。

5) Come back to the terminal in VS-CODE and type these commands in succession:

5)回到VS-CODE中的终端,依次输入这些命令:

git remote add origin <Link to GitHub Repo>     //maps the remote repo link to local git repo

git remote -v                                  //this is to verify the link to the remote repo 

git push -u origin master                      // pushes the commit-ed changes into the remote repo

Note: If it is the first time the local git account is trying to connect to GitHub, you may be required to enter credentials to GitHub in a separate window.

注意:如果是本地 git 帐户第一次尝试连接到 GitHub,您可能需要在单独的窗口中输入到 GitHub 的凭据。

6) You can see the success message in the Terminal. You can also verify by refreshing the GitHub repo online.

6) 您可以在终端中看到成功消息。您也可以通过在线刷新 GitHub 存储库进行验证。

Hope this helps

希望这可以帮助

回答by Xsmael

Install git on your PC and setup configuration values in either Command Prompt (cmd) or VS Code terminal (Ctrl + `)

在您的 PC 上安装 git 并在命令提示符 (cmd) 或 VS Code 终端 ( Ctrl + `) 中设置配置值

git config --global user.name "Your Name"
git config --global user.email [email protected]

Setup editor

设置编辑器

Windows eg.:

窗户例如:

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -nosession"

Linux / Mac eg.:

Linux / Mac 例如:

git config --global core.editor vim

Check git settings which displays configuration details

检查显示配置详细信息的 git 设置

git config --list

Login to github and create a remote repository. Copy the URL of this repository

登录到 github 并创建一个远程存储库。复制此存储库的 URL

Navigate to your project directory and execute the below commands

导航到您的项目目录并执行以下命令

git init                                                           // start tracking current directory
git add -A                                                         // add all files in current directory to staging area, making them available for commit
git commit -m "commit message"                                     // commit your changes
git remote add origin https://github.com/username/repo-name.git    // add remote repository URL which contains the required details
git pull origin master                                             // always pull from remote before pushing
git push -u origin master                                          // publish changes to your remote repository

回答by Steve L

I think I ran into the similar problem. If you started a local git repository but have not set up a remote git project and want to push your local project to to git project.

我想我遇到了类似的问题。如果您启动了本地 git 存储库但尚未设置远程 git 项目并希望将您的本地项目推送到 git 项目。

1) create a remote git project and note the URL of project

1)创建一个远程git项目并记下项目的URL

2) open/edit your local git project

2) 打开/编辑你的本地 git 项目

3) in the VS terminal type: git push --set-upstream [URL of project]

3)在VS终端输入:git push --set-upstream [项目的URL]

回答by Ryan Pereira

Yes you can upload your git repo from vs code. You have to get in the projects working directory and type git init in the terminal. Then add the files to your repository like you do with regular git commits.

是的,您可以从 vs 代码上传您的 git 存储库。您必须进入项目工作目录并在终端中输入 git init 。然后将文件添加到您的存储库,就像您使用常规 git 提交一样。

回答by Pavel L

There is a nice GUI way to do this. Press CTRL+SHIFT+G ( or View-CSM in menu) and here you have a lot of options. With (...) you can do almost anything you want. After things be done, type your commit message into input box and press CTRL+ENTER. Pretty easy. If you have remote repo - you'll see a little spinner mark in bottom left corner near repo name. Press it and sync to remote easily.
But in order to do all of this you must have repo to be initialized in your working directory before (git initfrom terminal).

有一个很好的 GUI 方法可以做到这一点。按 CTRL+SHIFT+G(或菜单中的 View-CSM),这里有很多选项。使用 (...) 你几乎可以做任何你想做的事情。完成后,在输入框中输入您的提交消息,然后按 CTRL+ENTER。挺容易。如果您有远程存储库 - 您会在存储库名称附近的左下角看到一个小旋转标记。按下它并轻松同步到远程。
但是为了完成所有这些,您必须在(git init从终端)之前在您的工作目录中初始化 repo 。

回答by Mario Nezmah

You can also use command palette:

您还可以使用命令面板:

  1. (CTRL+SHIFT+P - Win) or (CMD+SHIFT+P - Mac) to open the palette.
  2. Enter 'git', select Git:Clone,
  3. paste github repo URL (https://github.com/Username/repo),
  4. than you are ready to go with Source control section from the left menu.
  1. (CTRL+SHIFT+P - Win) 或 (CMD+SHIFT+P - Mac) 打开调色板。
  2. 输入'git',选择Git:Clone,
  3. 粘贴 github 仓库 URL ( https://github.com/Username/repo),
  4. 比您准备好使用左侧菜单中的源代码控制部分。

Does the same thing as the terminal.

与终端做同样的事情。

回答by Hiruni K

  1. create a new github repository.
  2. Goto the command line in VS code.(ctrl+`)
  3. Type following commands.
  1. 创建一个新的 github 存储库。
  2. 在 VS 代码中转到命令行。(ctrl+`)
  3. 键入以下命令。

git init

混帐初始化

git commit -m "first commit"

git commit -m "第一次提交"

git remote add origin https://github.com/userName/repoName.git

git 远程添加源https://github.com/userName/repoName.git

git push -u origin master

git push -u origin master

-

——

回答by Hitman

You can create a GitHub repo via the command line using the GitHub API. Outside of the API, there's no way to create a repo on GitHub via the command line.

您可以使用 GitHub API 通过命令行创建 GitHub 存储库。在 API 之外,无法通过命令行在 GitHub 上创建存储库。

Type:

类型:

curl -u 'username' https://api.github.com/user/repos-d '{"name":"projectname","description":"project desc"}'

curl -u '用户名' https://api.github.com/user/repos-d '{"name":"projectname","description":"project desc"}'

git remote add origin [email protected]:nyeates/projectname.git

git remote add origin [email protected]:nyeates/projectname.git

and now you can continue regular way

现在你可以继续正常的方式

回答by Sachin Kalia

Go to VS COde -> View -> Terminal

转到 VS Code -> 查看 -> 终端

enter image description here

在此处输入图片说明

git init git add . git commit -m "FirstCommit" git remote add origin https://github.com/dotnetpiper/cdngit pull origin master git push -f origin master

git init git add 。git commit -m "FirstCommit" git remote add origin https://github.com/dotnetpiper/cdngit pull origin master git push -f origin master

Note : Some time git push -u origin master doesn't work anticipated.

注意:有时 git push -u origin master 预期不起作用。