Git: Uploading an existing local project to GitHub from the console

Getting a local project into a public repository on GitHub only takes a few steps.  This is well documented on a GitHub help page.

In this article, I’ll go through getting a local project called “myproject1” into a public GitHub repository.

Create a GitHub repository

The first step is to create a new public repository named “myproject1”.  Follow the steps from this page.

For a project name in step 3, use “myproject1”.  Check “Initialize this repository with a README”, so that later you can see how to make this work.

After the repository is created, you will be at the main repository page.  Click on the “Clone or download” button to get the web URL, which should have a syntax similar to:

https://github.com/<user>/myproject1.git

Create a new local project

Now let’s create a local project that can be imported.

$ sudo apt-get install git -y
$ mkdir myproject1
$ cd myproject1
$ echo "sample content for myproject1" > myfile.txt

Initialize the local git repository

$ git init
$ git add .
$ git status
$ git commit -a -m "local import"

Configure for remote git repository

$ git remote add origin https://github.com/<user>/myproject1.git
$ git remote -v
$ git branch

Now, because we had earlier checked “Initialize this repository with a README” when we created the GitHub repository, we are going to need to pull from the remote repository before we make any commits.  The same would apply if we had asked GitHub to initialize the repository with a .gitignore or license file.  This could be skipped if we did not initialize any github files upon repository creation.

$ git pull https://github.com/<user>/myproject1.git master --no-edit

If there are errors saying ‘refusing to merge unrelated histories‘, append the ‘–allow-unrelated-histories’ switch.

Finally,  commit everything local to the remote repository by issuing a push

$ git push -u origin master

A refresh of the GitHub repository page should now look similar to below, where “myfile.txt” is in the public repository.

 

REFERENCES

https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

https://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories-on-rebase (force unrelated histories)

 

NOTES

To know where remote repository is located

git remote show origin

If push/pull says “The current branch master has no upstream branch”

git push --set-upstream origin master