Visual Studio Code - Connect to a remote Git repository and PUSH local files to a new remote repository

I have created a local project with Visual Studio Code that implements a local Git repository.

Then I have create a Git repository on Visual Studio Online and I want to push all my project files to the remote repository...

What is the correct procedure to do it?

My .git\config files at this moment look like this:

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

Solution 1:

I assume you started to work in a directory and didn't use Git there. The following should work with 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...

To add a remote, just do

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.

The -u sets an upstream reference and Git knows from where to fetch/pull and where to push in the future.

Solution 2:

It is now possible to set the origin from Visual Studio Code without command line typing:

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

From within Visual Studio Code you may Ctrl + SHIFT + P if you are on Windows and type remote.

From there select Git: Add Remote and you will have two steps:

  • Set origin for the remote name:
  • Add origin URL: https://github.com/USR/REPO.git

Solution 3:

From comments:

VSCode does not support the "add remote" action in its UI (April 2018)

Actually, it does since Visual Studio Code 1.46 (May 2020):

Add remote from GitHub

You can now easily add a GitHub repository as a remote to your local repositories using the Git: Add Remote... command.

https://media.githubusercontent.com/media/microsoft/vscode-docs/vnext/release-notes/images/1_46/git-add-remote.gif