0

I am a bit curious about pushing a locally created folder(project) to Github. Is it possible? Please let me explain what I have already done. I am using Git for Windows on Windows 8.1 64-bit.

  1. Created a new project using init command in the Git local root i.e. projects. Please refer the bash commands and messages below.

$ git init fresh-project Initialized empty Git repository in D:/MyDev/projects/fresh-project/.git/

2.

$ ls fresh-project/

  1. **$ cd fresh-project**

  2. **ls**

  3. **$ git status**
    

    On branch master

    No commits yet

    nothing to commit (create/copy files and use "git add" to track)

  4. **Used $ code hipster.txt**
    

    To create the file by opening VSCode and added some text.

  5. **$ git status**
    

    On branch master

    No commits yet

    Untracked files: (use "git add ..." to include in what will be committed) hipster.txt

    nothing added to commit but untracked files present (use "git add" to track)

  6. **$ git add hipster.txt**

  7. **$ git status**
    

    On branch master

    No commits yet

    Changes to be committed: (use "git rm --cached ..." to unstage) new file: hipster.txt

  8. **$ git commit**
    

    hint: Waiting for your editor to close the file... [main 2020-04-12T09:45:20.658Z] update#setState idle [main 2020-04-12T09:45:50.660Z] update#setState checking for updates [main 2020-04-12T09:45:50.870Z] update#setState idle [master (root-commit) b427f7c] Adding new file with hipster ipsum This was done with VSCode in 12th April 2020 15:20 1 file changed, 1 insertion(+) create mode 100644 hipster.txt

11.

$ git status On branch master nothing to commit, working tree clean

After the 11th step I want to push the whole local repository to Github.com. I haven't created a 
repository in Github with the name of 'fresh-project'.

What else do I have to do to make this to happen?

Thanks and regards, Chiranthaka

ChiranSJ
  • 195
  • 1
  • 3
  • 20
  • Does this answer your question? [Import existing source code to GitHub](https://stackoverflow.com/questions/4658606/import-existing-source-code-to-github) – phd Apr 12 '20 at 15:19
  • https://stackoverflow.com/search?q=%5Bgit%5D+push+directory – phd Apr 12 '20 at 15:19

2 Answers2

1

You should have searched on github for this. It's easy all you need to do is first add files and commit then add remote repo url and push to github

$ git remote add origin https://github.com/user/yourremoterepo.git

If you want to create repo using command line you need to use Github api for that You need an access token and curl to POST repo

curl -H "Authorization: token ACCESS_TOKEN" --data '{"name":"Reponame"}' https://api.github.com/user/repos
Bibek
  • 943
  • 1
  • 12
  • 22
  • Thanks @Bibek but I already used the command once like this way $ git remote add origin https://github/chiranthakaj/fresh-project.git I got the error like the below fatal: remote origin already exists. But when I refresh the repo in Github all repo view I can't see the 'fresh-project' repository. – ChiranSJ Apr 12 '20 at 12:10
  • please refer the image in the link https://drive.google.com/open?id=1Hat-qs6932nSI19cJ6BFhX4Omkcmx32X – ChiranSJ Apr 12 '20 at 12:17
  • I guess you haven't created fresh-project repo for that. First create a repo on github then gitinit your local project and add remote origin and then you can push your code – Bibek Apr 12 '20 at 12:18
  • Remember you cannot add origiin without initializing repository on github either manually or by API – Bibek Apr 12 '20 at 12:20
  • Thanks @Bibek for the answers. – ChiranSJ Apr 12 '20 at 12:36
0

As already mentioned, when you create a new repository on Github, it displays steps on how you push an existing local project/repository from command line:

git remote add origin https://github.com/username/yourremoterepo.git
git push -u origin master

Furthermore, you can also use -v as below to showcase the URLs git has stored to be used for reading/writing to that remote repo:

git remote -v
origin  https://github.com/username/yourremoterepo (fetch)
origin  https://github.com/username/yourremoterepo (push)

If this isn't pointing to your required project (fresh-project), there's a high likelihood that the project was never created on Github. Or the steps to create the remote weren't followed correctly.

Assuming you're a beginner to git as myself, I would recommend to start reading the Pro-git book. It can be found here. For reference on working with remotes, see section 2.5