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

GitVisual Studio-Code

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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 - Git

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:

Solution 3 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarioN1View Question on Stackoverflow
Solution 1 - GitChristophView Answer on Stackoverflow
Solution 2 - GitprostiView Answer on Stackoverflow
Solution 3 - GitVonCView Answer on Stackoverflow