Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Existing Project To Git Repo

#Adding an existing project to GitHub using the command line

Simple steps to add existing project to Github.

1. Create a new repository on GitHub.

In Terminal, change the current working directory to your local project.

##2. Initialize the local directory as a Git repository.

git init

Add the files in your new local repository. This stages them for the first commit.

git add .

or:

git add --all

Commit the files that you've staged in your local repository.

git commit -m 'First commit'

Copy remote repository URL field from your GitHub repository, in the right sidebar, copy the remote repository URL.

In Terminal, add the URL for the remote repository where your local repostory will be pushed.

git remote add origin <remote repository URL>

Sets the new remote:

git remote -v

Push the changes in your local repository to GitHub.

git push origin master

Pushes the changes in your local repository up to the remote repository you specified as the origin

@eds-satish

This comment has been minimized.

Copy link

commented Aug 2, 2017

thank you it worked for me.

@MnTronslien

This comment has been minimized.

Copy link

commented Aug 15, 2017

Does this work when migrating an existing project as well?

@lreiner

This comment has been minimized.

Copy link

commented Aug 17, 2017

Thank you worked for me too

@isaacayz

This comment has been minimized.

Copy link

commented Sep 6, 2017

It's giving me errors.

@andikaerwansyah

This comment has been minimized.

Copy link

commented Sep 18, 2017

it works perfectly, thanks, man.

@AnujGangwal

This comment has been minimized.

Copy link

commented Sep 20, 2017

Worked for me, thanks.

@PosSoolutions

This comment has been minimized.

Copy link

commented Sep 24, 2017

Worked for me

@jurgens37

This comment has been minimized.

Copy link

commented Oct 4, 2017

I enter : git push origin master
then : I don't handle 'https'

how to fix it ?

@nirmalsi

This comment has been minimized.

Copy link

commented Nov 1, 2017

can i change from master branch.

@AyoubGharbi

This comment has been minimized.

Copy link

commented Nov 11, 2017

Thanks :)

@kevoh3

This comment has been minimized.

Copy link

commented Nov 16, 2017

it works for me thanks

@charlescampista

This comment has been minimized.

Copy link

commented Nov 26, 2017

I got an error.
So I've changed "git push origin master" for this "git push -f github master".
it works well for me.

@OldboyJ

This comment has been minimized.

Copy link

commented Nov 28, 2017

It completely worked for me..
👍

@TarunKShukla

This comment has been minimized.

Copy link

commented Nov 30, 2017

It's really helpful.Thanks

@keshari1arya

This comment has been minimized.

Copy link

commented Dec 2, 2017

remote: Permission to keshari1arya/Lipun.git denied to arya-choudhury.
fatal: unable to access '{my url}': The requested URL returned error: 403

i am getting this error while pushing. please help me

@benoitrolland

This comment has been minimized.

Copy link

commented Dec 2, 2017

Had to force, because remote project seems to have its own history (automated Licence.md and README.md creation ?):
git push -f origin master

@TimEaleifo

This comment has been minimized.

Copy link

commented Dec 5, 2017

Hi,
This didn't work for me. i got the message below-- please any solution

C:\Tim\source\repos>git push -u origin master
To https://github.com/hester/TimSSIS.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/hester/TimSSIS.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.

@Zerka1982

This comment has been minimized.

Copy link

commented Dec 7, 2017

Hello TimeEaleifo,

You can simply create a new repository without adding README.MD or gitignore files.
Then move your project to another folder. Right click on the project and choose "Git Bash here". Run the following command lines :

1- git init
2- git add .
3- git commit -m "first commit"
4- git remote add origin "remote repository URL"
5- git push origin master

I hope this will help u solving the problem. Good Luck!

@zhaohany

This comment has been minimized.

Copy link

commented Dec 15, 2017

! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/zhaohany/Rutgers-CS211.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.

@SalathielGenese

This comment has been minimized.

Copy link

commented Jan 4, 2018

Please, wrap "First commit" around double quotes.

Thanks for sharing.

@deepakrishnan90

This comment has been minimized.

Copy link

commented Jan 19, 2018

error: src refspec Regression does not match any.

I am getting above mentioned error. Please help

@weitzhandler

This comment has been minimized.

Copy link

commented Jan 23, 2018

Can't get it to work:

D:\Users\Shimmy\Source\Repos\MyRepo>git push origin master
To https://github.com/weitzhandler/MyRepo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/weitzhandler/MyRepo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

D:\Users\Shimmy\Source\Repos\MyRepo>
@polvilarrasa

This comment has been minimized.

Copy link

commented Jan 25, 2018

i don't understand what i have to do in this part:
Copy remote repository URL field from your GitHub repository, in the right sidebar, copy the remote repository URL.

In Terminal, add the URL for the remote repository where your local repostory will be pushed.

i am quite new to this

@mtalha91

This comment has been minimized.

Copy link

commented Feb 3, 2018

I am having an error when push: Fatal error: no repository found. I have also check whether I have successfully setup ssh key

@franzabgrund

This comment has been minimized.

Copy link

commented Feb 7, 2018

Hi, after command git commit -m "first commit" I get the following error:

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address 
@abyabrkal

This comment has been minimized.

Copy link

commented Feb 11, 2018

I had the same as @TimEaleifo, while pushing.

Force push worked.
git push -u -f origin master

@Divi28

This comment has been minimized.

Copy link

commented Feb 13, 2018

Thank you..It Worked for me :)

@ngarciaiii

This comment has been minimized.

Copy link

commented Feb 25, 2018

abyabrkal's worked for me. :) Thanks!

@danipralea

This comment has been minimized.

Copy link

commented Mar 9, 2018

thank you! worked!

@lukehinds

This comment has been minimized.

Copy link

commented Apr 19, 2018

So this is for importing existing code, not an existing git repo with a .git set of files already present.

@AlexayEugine

This comment has been minimized.

Copy link

commented Apr 21, 2018

I have some issue:

E:\GitLab\proguiden\laravel.proguiden.no>git push origin master
To https://gitlab.com/FantasyLab/laravel.proguiden.no.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitlab.com/FantasyLab/laravel.proguiden.no.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

How can I solve it?

@bufpal

This comment has been minimized.

Copy link

commented May 6, 2018

Thank you, it worked for me too :)

@timzzy

This comment has been minimized.

Copy link

commented Jun 11, 2018

Workss....whoolaaaaa

@emoonadev

This comment has been minimized.

Copy link

commented Jun 28, 2018

Works fineeee thanks

@sushant-78

This comment has been minimized.

Copy link

commented Jul 16, 2018

thanks.

@molebox

This comment has been minimized.

Copy link

commented Aug 3, 2018

Boom! Yes mate, great guide!

@AGudyma

This comment has been minimized.

Copy link

commented Aug 8, 2018

cool very useful, simple to understand and to use. Thanks Dude!

@urwa

This comment has been minimized.

Copy link

commented Aug 17, 2018

FOR FUTURE READERS

! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/zhaohany/Rutgers-CS211.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.

This can be solved by using git push -f origin master instead of git push origin master OR creating a repo on github.com WITHOUT a readme.md

*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address

This can be solved by running above two commands by replacing you@example.com with your email id and Your Name by your name on github.com. Both of these can be found on your github.com profile or settings

error: pathspec 'Commit' did not match any file(s) known to git.

This can be resolved by using " " around Commit like this git commit -m "First Commit"

Copy remote repository URL field from your GitHub repository, in the right sidebar, copy the remote repository URL.
In Terminal, add the URL for the remote repository where your local repository will be pushed.

This is simply telling you to go to your repo on github.com where you want to move your code files and simply copy the URL. Ignore the sidebar thing etc ( I didn't get it myself )

Hope it helps.

@ZoeyOneOhOne

This comment has been minimized.

Copy link

commented Sep 12, 2018

I get this error, "bash: syntax error near unexpected token `newline'" what does this mean?

@oudoulj

This comment has been minimized.

Copy link

commented Sep 28, 2018

For Windows users, whenever you see single quotes in the guide above, replace them with double quotes eg
git commit -m "First commit"

@akanshamalik

This comment has been minimized.

Copy link

commented Oct 9, 2018

i am not able to add files, it says this:
error: open(".cache/tilda/locks/lock_3846_0"): Permission denied
error: unable to index file .cache/tilda/locks/lock_3846_0
fatal: adding files failed

@leminhhung

This comment has been minimized.

Copy link

commented Nov 22, 2018

Thanks, work for me!

@orionhall

This comment has been minimized.

Copy link

commented Jan 26, 2019

Hi, I just tried to do this on my own and I believe I followed the same steps, but when I tried to push I was told I had to pull (probably because I initialized the repo with a readme file), and on doing that was told it couldn't be done because of "cannot merge unrelated histories". I eventually pulled with allow-unrelated-histories, but then had to enter a message as to why I was merging, etc. Is this because I added the readme file or does anyone else know what I may have done wrong?

@akashgudadhe1

This comment has been minimized.

Copy link

commented Feb 15, 2019

It worked...!!
Thanks for such a nice tutorial.

@veerpalk

This comment has been minimized.

Copy link

commented Feb 22, 2019

Hi,
This didn't work for me. i got the message below-- please any solution

C:\Tim\source\repos>git push -u origin master
To https://github.com/hester/TimSSIS.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/hester/TimSSIS.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.

Hi , You can use command:
git push origin master --force

@prashant1k99

This comment has been minimized.

Copy link

commented Mar 1, 2019

can i change from master branch.

git remote add github https://github.com/username/repo.git
It will add github as your commit name.
So if you want to commit to github...git push github

You can even connect multiple platforms with the single Repo..

@prashant1k99

This comment has been minimized.

Copy link

commented Mar 1, 2019

I have some issue:

E:\GitLab\proguiden\laravel.proguiden.no>git push origin master
To https://gitlab.com/FantasyLab/laravel.proguiden.no.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitlab.com/FantasyLab/laravel.proguiden.no.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

How can I solve it?

You need to make some additional changes to the push or need to add commit before performing the Push.

@prashant1k99

This comment has been minimized.

Copy link

commented Mar 1, 2019

Hi, after command git commit -m "first commit" I get the following error:

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address 

You need to authenticate first then the commit will be accepted.

@buerlee

This comment has been minimized.

Copy link

commented Mar 1, 2019

Had to force, because remote project seems to have its own history (automated Licence.md and README.md creation ?):
git push -f origin master

It works. Thanks.

@AlessandraZullo

This comment has been minimized.

Copy link

commented Mar 14, 2019

Hi,
This didn't work for me. i got the message below-- please any solution

C:\Tim\source\repos>git push -u origin master
To https://github.com/hester/TimSSIS.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/hester/TimSSIS.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.

git push -f origin master

@KidOfCubes

This comment has been minimized.

Copy link

commented Apr 24, 2019

It works, thx :D

@KidOfCubes

This comment has been minimized.

Copy link

commented Apr 24, 2019

It works, thx :D

@RikTheGuy

This comment has been minimized.

Copy link

commented May 23, 2019

Thanks

@mattpenner

This comment has been minimized.

Copy link

commented May 24, 2019

Perfect! I use VS Code and attempted to do the commands from the Command Pallet, but most of the parameters mentioned above weren't recognized so I resorted to doing those commands in the terminal. Worked great.

@syamsidhardh

This comment has been minimized.

Copy link

commented Jun 12, 2019

How to migrate an existing project from git kernel to github without loosing the commit log history.
For ex: https://git.kernel.org/pub/scm/bluetooth/bluez.git to https://github.com/username/bluez.git with all the commit history preserved

@vadramson

This comment has been minimized.

Copy link

commented Jun 17, 2019

To Initialize your local project directory as a Git repository;
-> Open your command prompt and navigate to the project directory
-> Then enter

git init

Add the files in your new local repository. This stages them for the first commit.

git add .
or:

git add --all
Commit the files that you've staged in your local repository.

git commit -m 'First commit'
Copy remote repository URL field from your GitHub repository, in the right sidebar, copy the remote repository URL.

In Terminal, add the URL for the remote repository where your local repostory will be pushed.

git remote add origin
Sets the new remote:

git remote -v
Push the changes in your local repository to GitHub.

git push origin master
Pushes the changes in your local repository up to the remote repository you specified as the origin

@mdhe1248

This comment has been minimized.

Copy link

commented Jul 5, 2019

I have some issue:

E:\GitLab\proguiden\laravel.proguiden.no>git push origin master
To https://gitlab.com/FantasyLab/laravel.proguiden.no.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitlab.com/FantasyLab/laravel.proguiden.no.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

How can I solve it?

I think this issue occurs when the newly generate remote repository (or existing repository) already contains some files (e.g. readme.md and .gitignore). One solution is to use git push -f origin master.
Another solution may be to run git pull origin master. This way, you can keep the existing remote files.
Before running the commands below, it might be safer to create a new branch. There is possibility of having some problems.

In your local project directory e.g.) cd /home/usr/new_project,

  1. git init
  2. git remote add origin your_github_repository
  3. git pull origin master
  4. git add --all
  5. git commit -m "First commit"
  6. git push origin master
@alejoes

This comment has been minimized.

Copy link

commented Jul 25, 2019

I'm trying to do it through Github Desktop and the click label does not change color so I can click and proceed

@zneti-sourour

This comment has been minimized.

Copy link

commented Aug 2, 2019

hi , when i do the cmd ( git remote add origin )i have this message:
fatal: remote origin already exists.
what do you think the problem is ?

@mdhe1248

This comment has been minimized.

Copy link

commented Aug 2, 2019

hi , when i do the cmd ( git remote add origin )i have this message:
fatal: remote origin already exists.
what do you think the problem is ?

My guess is the origin has already been added.
git remote -v will give you the list of origin. Assuming that you have already added the origin, it may be okay to skip to the next step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.