158

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

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

EDIT 2020 :

You can now do it right inside vscode! just follow these steps:

1- Open your new project folder with vscode

2- click on the source conrol menu on the sidebar enter image description here (or press Ctrl+Shift+G)

3- Click on publish to github enter image description here

4- From there just login and follow the instructions and you're good to go.

@Debu's answer details every step, so you can jump to there

5
  • that is the right way. and easy to do using GUI.
    – Jinna Balu
    Oct 22, 2017 at 18:37
  • 1
    But its less convenient... IMO
    – Xsmael
    Oct 22, 2017 at 19:05
  • What do you mean by "Star"?
    – ifconfig
    Oct 22, 2017 at 19:58
  • @ifconfig am sorry that was a typo i meant "start" i corrected it
    – Xsmael
    Oct 28, 2017 at 22:09
  • 1
    You can use the command palette Ctrl+Shift+P and search there for publish to Github. Jul 2, 2021 at 2:27

17 Answers 17

193

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.

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

    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

    git commit -am "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.

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

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

    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.

  1. You can see the success message in the Terminal. You can also verify by refreshing the GitHub repo online.
6
  • 10
    is it possible to make a vscode extension that does all these commands ?
    – Xsmael
    Jan 27, 2019 at 7:59
  • @VikramK thanks for your answer it is clearly useful to many people. but this is not exactly what i wanted. these are commands and can be run from any other terminal than the one vscode has. I was expecting a GUI way of doing it, you know the same way you do commits in vs-code with the git extension. So I was thinking about the possibility to create th repo from vscode(not the integrated terminal). Hope I make sense...
    – Xsmael
    Apr 9, 2019 at 20:09
  • @Xsmael you could have mentioned that in the question title clearly then..However I'll look into this as well
    – Vikram K
    Apr 26, 2019 at 15:12
  • 5
    I had to use --force in the push command because I selected a LICENSE on GitHub while creating the new repo and my local Git refused to merge the changes. Nov 20, 2019 at 12:46
  • 1
    don't miss git add. between step 1 & step 2 Aug 16, 2022 at 14:54
51

This feature was added in 1.45, demoed here.

Launch the command palette Ctrl+Shift+P, run Publish to Github, and follow the prompt. You will be given the choice between a private and public repository, so be careful that you choose the right one.

running from command palette

It may ask you to login to github. It will then prompt for the repo name (defaults to the name of the folder), and for creating a .gitignore file (defaults to empty .gitignore). Just hit enter if you are fine with the defaults. When you are done it should give you a popup notification in the bottom right with a link to the repo https://github.com/<username>/<reponame>

Minor warning: if your project already has a .gitignore file in it this process will overwrite it

4
  • 7
    This was the exact answer I was looking for. Specifically adding a new project to github without leaving the Visual Studio Code interface.
    – Mifo
    Jul 26, 2020 at 14:38
  • 4
    This should be the actual, correct answer to the actual question. I understand this feature didn't always exist but I feel like this is what the original poster was looking for rather than all of the command line answers.
    – cfont
    Jul 28, 2020 at 22:50
  • This should be marked as the correct answer. Besides, it is simple and straight forward
    – Tee
    Jan 15, 2021 at 3:00
  • Yes. This should be marked as Answer. Earlier when project was created I will delete .git folder and then open VS Code. Then only I will get the "Initialize the repository" & "Publish To GitHub" options. Now with "Ctrl+Shift+P" and then "Publish to Github" we can achieve the OP required functionality Feb 21, 2021 at 10:47
34

Well, It's quite easy.

Open your local project.


VSCode source control interface showing Open Folder and CLone Repository


Add a README.md file (If you don't have anything else to add yet)


VSCode explorer interface showing README.md


Click on Publish on Github


VSCode source control interface showing Initialize Repository and Clone to GitHub


Choose whether you want your repo to be private or public


Interface showing choice between private and public repos


Choose the files you want to include in first commit.
Note: If you don't select a file or folder it will added to .gitignore file


Showing interface where you choose which files to include in the repo


You are good to go. it is published.

P.S. If this was your first time a prompt will ask for for your Github Credentials. Fill those in and you are good to go. It is published.

7
  • When to choose initialize repository and when to choose publish to github ? Jan 17, 2021 at 14:48
  • Initializing Repository will create a .git folder inside the local repo, which will help you manage code using git file system. But, please note you have this code in your local, not on the cloud. So if your system crashes everything vanishes. But to deal with that situation you can publish the repo to GitHub. Everything will be the same as above but now you have the code on the cloud. Jan 20, 2021 at 19:05
  • 3
    Ctrl + Shift + P, Type Publish to GitHub.
    – Raphael
    Jul 10, 2021 at 8:11
  • 1
    Worked great, just needed the "GitHub Pull Requests and Issues extension". for that just type on browser vscode:extension/GitHub.vscode-pull-request-github Jul 16, 2021 at 6:26
  • @GiridharanVenkatapathy yep already have it. Thanks! Jul 16, 2021 at 12:31
22

Install git on your PC and setup configuration values in either Command Prompt (cmd) or VS Code terminal (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.:

git config --global core.editor vim

Check git settings which displays configuration details

git config --list

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

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
12

today is 2020-12-25, my VSC is 1.52.1, tried all above not very successful. Here is complete steps I did to add my existing local project to GitHub using VSC (Note: Do not create a corresponding repository at GitHub):

  1. Install the GibHub extension to VSC.
  2. Close and re-open VSC.
  3. Sign in to GibHub if prompted.
  4. Open my own local folder, up to this moment, it's not pushed to GibHub yet.
  5. F1, click on Publish to GitHub (or type in if not prompted).

enter image description here

  1. You will be provided a choice of adding it as private or public, so make up a name for your to-be new repository in this format: <your username>/<your new repository name>. For example, my username is "myname" and my local folder is named "HelloWorld". it will be myname/HelloWorld in the type-in box.
  2. update or accept this name, click on the private or public choice will Create a new repository at GitHub and Publish your folder to it.

enter image description here

10

Push code on git hub in V S code

git init

git add .

git commit -m " test this app"

git remote add origin https://github.com/username/flutterapp.git

git push or 

git push --set-upstream origin master
7
  1. Create a new GitHub repository.

  2. Go to the command line in VS code (ctrl+`).

  3. Type following commands:

    git init
    git commit -m "first commit"
    git remote add origin https://github.com/userName/repoName.git
    git push -u origin master
    
4

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.

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

2) open/edit your local git project

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

3

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 init from terminal).

2
  • 1
    you didnt bring in anythin new
    – Xsmael
    Nov 3, 2017 at 11:54
  • fatal: no upstream configured for branch 'master' Jul 22, 2018 at 7:44
2

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.

3
  • while, this works, its not really from vs code as you mentioned.
    – Xsmael
    Oct 28, 2017 at 22:08
  • Its just a git command which is run on terminal. As vs code allows you to access the terminal from the editor itself, you don't have to switch between tabs. Oct 30, 2017 at 9:15
  • 1
    okay, so there is no "GUI-way" to do it like for the commit or push in vs code
    – Xsmael
    Oct 30, 2017 at 21:46
2

Go to VS COde -> View -> Terminal

enter image description here

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

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

1

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.

Does the same thing as the terminal.

1

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.

Type:

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

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

and now you can continue regular way

1

Here are the commands you can use to add a new project to GitHub using VS Code:

git init
git add .
git commit -m "Initial commit"
git remote add origin <repository URL>
git push -u origin master

If you face any issue like fatal: repository not found, check your repository url and check whether you are authenticated.

Note: You should run these commands from the terminal within VS Code and make sure you are in the correct directory of your project. And also you need to make sure that you are authenticated before running the last command.

0

I have followed the below steps to add or export an existing project to GitHub and it should work for all the environments.

  1. Create a new Repo in GitHub then copy the repo URL
  2. Open the terminal in VS or Gitbash
  3. You should be in the project path / working directory
  4. git init //to initialize git on local directory
  5. git add . // add your project
  6. git commit -m "YOUR MESSAGE"
  7. git remote add origin
  8. git remote -v
  9. git push -f origin master // to push your changes to Git repo
0

I followed all the steps from VS CODE only:

  1. Open command pallet (cmd+shift+p), do git initialized repository.

  2. Open command pallet (cmd+shift+p), do git add and commit.

  3. Open command pallet (cmd+shift+p), do git add remote, then you will have a screen like this, do the click as mentioned:

    enter image description here

    You will have all the repository available on your GitHub account, this would help you add remote from vs code only, considering repository is already present on your GitHub account.

  4. Open command pallet (cmd+shift+p), do git push, it would ask you for private or publish repository choose whatever you like.

We could have used the terminal from vscode as mentioned by others but I find more intuitive doing this way.

Note - VS code would ask you in-between to authorized your vscode for your GitHub account uses.

-2

What should you do, delete .git folder from your app/ folder delete large files or move somewhere else, in my case I don't need it. then git init again

1
  • 1
    I do not see how this answers the question at the top of this page, but it should. It starts like a question instead of an answer, then changes to seeming a description of your problem. It also suffers from lack of helpful formatting. (See stackoverflow.com/editing-help ) Please edit according to How to Answer or delete the answer. Otherwise it risks being flagged as "not an answer" and being deleted.
    – Yunnosch
    Sep 28, 2021 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.