Jerielle
Jerielle

Reputation: 7520

How to push an existing projects in new Github repository?

I am studying git now and I have a problem in pushing my existing files in the new repository I created in Github.

Here's what i did:

  1. I perform this inside my project - git init
  2. I added the files in the local repository - git add .
  3. I commit - git commit -m "This is the base code for the project"
  4. Then I copy the github repository URL in my Github page https://github.com/myname/my_projects.git
  5. Then I point out the repository - git remote add origin https://github.com/myname/my_projects.git
  6. Then I push my files - git push origin master

Then after doing the push I have this error:

λ git push origin master
Username for 'https://github.com': myname
Password for 'https://myname@github.com':
To https://github.com/myname/my_projects.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/myname/my_projects.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 integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Can you help me with this?

Upvotes: 1

Views: 140

Answers (2)

ifma
ifma

Reputation: 3818

You probably initialized the repo on Github with a README file. If so, you can do as the earlier answer suggested, or you can pull first with:

git pull origin master

Your branch is not up to date since the remote has a README file and you don't have this file locally. After you pull, you would have to commit again, but what this would do is tell Git that your local branch to be up to date with the remote branch.

After this point you can just do push as usual:

git push origin master

Upvotes: 2

Petr Skocik
Petr Skocik

Reputation: 60163

git push --force origin master if you're sure you want to overwrite what's on your github remote with what you have locally.

Upvotes: 3

Related Questions