Push existing project into Github

I have a folder with my project sources. How I can push this project into Github's repository?

I tried using this steps:

  1. I created empty repository on GitHub.
  2. I run git-bash and typed git init, so inside project root appeared .git folder.
  3. I added some files to version control using git add sourcesFolderName
  4. I committed files added in previous step using git commit -m "initial commit"
  5. I specified remote repository using git remote add MyProject <url>
  6. Finally git push, but nothing is pushed to remote repo... (no authorization failure)

So how I can push existing sources into newly created github repo?


git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master

The -f option on git push forces the push. If you don't use it, you'll see an error like this:

To [email protected]:roseperrone/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:roseperrone/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

In less technical terms

My answer is not different but I am adding more information because those that are new could benefit from filling in the gaps in information.

After you create the repo on github they have instructions. You can follow those. But here are some additional tips because I know how frustrating it is to get started with git.

Let's say that you have already started your project locally. How much you have does not matter. But let's pretend that you have a php project. Let's say that you have the index.php, contact.php and an assets folder with images, css, and fonts. You can do it this way (easy), but there are many options:

Option 1

Login to your github account and create the repo.

enter image description here

In the following screen you can copy it down where you need it if you click the button (right side of screen) to "clone in desktop".

enter image description here

You can (or do it another way) then copy the contents from your existing project into your new repo. Using the github app, you can just commit from there using their GUI (that means that you just click the buttons in the application). Of course you enter your notes for the commit.

Option 2

  • Create your repo on github as mentioned above.
  • On your computer, go to your directory using the terminal. using the linux command line you would cd into the directory. From here you run the following commands to "connect" your existing project to your repo on github. (This is assuming that you created your repo on github and it is currently empty)

first do this to initialize git (version control).

git init

then do this to add all your files to be "monitored." If you have files that you want ignored, you need to add a .gitignore but for the sake of simplicity, just use this example to learn.

git add .

Then you commit and add a note in between the "" like "first commit" etc.

 git commit -m "Initial Commit"

Now, here is where you add your existing repo

git remote add github <project url>

But do not literally type <project url>, but your own project URL. How do you get that? Go to the link where your repo is on github, then copy the link. In my case, one of my repos is https://github.com/JGallardo/urbanhistorical so my resulting url for this command would just add .git after that. So here it would be

git remote add github https://github.com/JGallardo/urbanhistorical.git

Test to see that it worked by doing

git remote -v

You should see what your repo is linked to.

enter image description here

Then you can push your changes to github

git push github master

or

git push origin master

If you still get an error, you can force it with -f. But if you are working in a team environment, be careful not to force or you could create more problems.

git push -f origin master

you will need to specify which branch and which remote when pushing:

➤ git init ./
➤ git add Readme.md
➤ git commit -m "Initial Commit"
➤ git remote add github <project url>
➤ git push github master

Will work as expected.

You can set this up by default by doing:

➤ git branch -u github/master master

which will allow you to do a git push from master without specifying the remote or branch.