Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push existing project into Github

Tags:

git

github

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.


If you're on a Mac (and this probably works the same on a PC), here's a very easy way to do this. Strangely enough I've looked high and low for this simple process and never found it.

  • Do not do anything on Github (other than having an account, and not having used up all your available repos).
  • Download GitHub for Mac and install. Go through the account setup, etc. Do NOT create any repositories for your existing project.
  • "Add New Local Repository" in repositories.
  • Select your existing folder. It'll ask if you want to do that, say yes.
  • Once done, you'll see a list of all your files, etc. Commit them.
  • Go to Repositories and Publish (this will create the new repo on GitHub for you, if you set up your account properly).
  • Go to Repositories and Push (you'll either see the "nothing to push" thing, or it'll push your files/changes to the newly-auto-made repo).
    • Wonder why you could not find this simple process anywhere else.

I know it is not recommended to use the project folder as the repo folder. I do it all the time, it always works, it makes it simple, and I never have any trouble with it.