57

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DarioN1
  • 2,460
  • 7
  • 32
  • 67
  • 1
    VSCode does not support the "add remote" action in its UI. So, I also had to do it manually like @Christoph suggests – Val Martinez Apr 04 '18 at 10:06
  • I looked and I looked and then I found this post. If you have already setup your solution in VS Code and then create your GitHub repo and want to push your code to the new repo this answer will do it. – gkayton Jul 12 '19 at 15:07

4 Answers4

99

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christoph
  • 6,841
  • 4
  • 37
  • 89
  • Thanks Christoph, I will be the only one who will access the repository and I will not need to create branches for sure. That "origin" in the first line, what indicates? – DarioN1 Apr 12 '17 at 08:50
  • 2
    It is just an abbreviation for the whole address. You can give it any name, but most call it `origin` if they only have one remote address. You should read [this](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes) ;-) – Christoph Apr 12 '17 at 08:54
  • Thanks, just another simple question: is it a correct procedure to define the local repository, copy the files and then run the command that you provide in order to pubblish the files on the remote repository ? Or there is a better way to do that ? – DarioN1 Apr 12 '17 at 08:58
  • 2
    You do not need to copy anything! Why do you want to do that? – Christoph Apr 12 '17 at 09:00
  • Because I have created a blank repository and I have to add lot of files... – DarioN1 Apr 12 '17 at 09:03
  • Thanks Christoph, it works. So basically that command on third row "git add ." adds all path files and folder to the local repository right ? And if in the future I have to add another folder I just need to send command "git add .\folderToAdd" right? – DarioN1 Apr 12 '17 at 09:22
  • 3
    I strongly recommend to read through [this](https://git-scm.com/docs). If something remains unclear, just write a question on SO ;-) It doesn't make sense to start another tutorial. – Christoph Apr 12 '17 at 09:25
  • I agree! Thanks Christop, I very apreciate your help! – DarioN1 Apr 12 '17 at 09:26
22

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:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prosti
  • 42,291
  • 14
  • 186
  • 151
5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 4
    After it did the autocomplete on my git project, I selected it and hit enter. Then it prompted me with the blank edit box again. No message. Just not working. – lurker Mar 24 '21 at 16:42
0

It may work on GitHub but not on an other server. I have my own server and work with an own server. I still have to create the repository by my own, like discribed here , the I can connect to the repo and very thing works fine. I can sync from my desktop to the sever and pull and push on my laptop out of vscode

Mohammad abumazen
  • 1,286
  • 1
  • 11
  • 24
Rainer
  • 1