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

@mdhe1248
Copy link

mdhe1248 commented May 14, 2021

I'm creating my all projects through superuser, I'm unable to push code to github. Please help.

This might be helpful?
https://github.com/git-guides/git-push
Especially, "Why can't I push?" in the link above.

@mdhe1248
Copy link

mdhe1248 commented May 14, 2021

It is creating a branch 'master' and that branch I am not able to merge into the 'main' branch. (It show nothing in common so cannot merge)
Sorry if it is a silly question, but I am trying this thing and stuck since many days. Any help would be much appreciated.

I guess.. if you provide more information, people here could help you! For example, actual commands and error messages. And how did you create your master branch and main branch. And so on..

@meet97
Copy link

meet97 commented May 18, 2021

It worked for me
Thanks

@rickybell
Copy link

rickybell commented May 23, 2021

Good work man.

@j-saurabh
Copy link

j-saurabh commented May 24, 2021

I got the solution. I was trying to push to a project which had no link with each other. That is I had a local project and I made an empty repository in github, I should have cloned the repo, moved the files in that cloned repo in my local directory and then tried to push.
Now it is working fine. Thank you!

@vishnu92karthik
Copy link

vishnu92karthik commented May 26, 2021

Thank you, Worked for me.

@BaseMax
Copy link

BaseMax commented Jun 4, 2021

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

@dheerajrp
Copy link

dheerajrp commented Jun 6, 2021

Thanks. Worked for me

@gitanupam
Copy link

gitanupam commented Jul 17, 2021

Thanks. Straightforward

@HaleemaEssa
Copy link

HaleemaEssa commented Jul 25, 2021

Thanks. Worked for me

@humbertoibanez
Copy link

humbertoibanez commented Aug 24, 2021

works for me after ssh remote, mkdir project_name, cd project_name and git init --bare.

@Celqaz
Copy link

Celqaz commented Aug 28, 2021

Thanks for the clear steps. Worked for me.

@anar1501
Copy link

anar1501 commented Sep 24, 2021

thanks very much

@ml-alchimist
Copy link

ml-alchimist commented Oct 11, 2021

Thank you

@BegenchO
Copy link

BegenchO commented Oct 15, 2021

Thank you. Worked perfectly. First time user

@dinhtona
Copy link

dinhtona commented Nov 14, 2021

Thank you

@ZihadHosan
Copy link

ZihadHosan commented Dec 8, 2021

Thank you so much!

@semargub
Copy link

semargub commented Mar 30, 2022

thanks a lot

@Shoogn
Copy link

Shoogn commented Apr 16, 2022

This will remove the current files in the remote branch, be careful with this command

@martillo675
Copy link

martillo675 commented May 8, 2022

thanks you so much!

@cameronmcnz
Copy link

cameronmcnz commented May 28, 2022

I normally add the -f and -u switch on the push.

git push -u -f origin main

I put together a video on how to push existing projects to GitHub for any visual learners out there who need some guidance.

https://www.youtube.com/watch?v=gHgidwb50lQ

@malikadilofficial
Copy link

malikadilofficial commented Jun 8, 2022

Thanks! such a simple guide for a complex issue.

@thedivloop
Copy link

thedivloop commented Jun 24, 2022

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.

Force it:
git push --set-upstream origin master --force

This might happen if you create a new github repo with a readme file. It could also be totally different code and somehow you feel confident to merge. Then you can do a fetch of the github repo:

git fetch

Followed by a merge with --allow-unrelated-histories:

git merge origin/main --allow-unrelated-histories

When the commit file opens just save it or update it.
Then you are ready for the push

git push origin main

This will sync your local repo with your remote repo (origin on branch main).

Here I used main because github is now using main by default instead of master.
If locally your HEAD is on master then you can rename the branch so:

git branch -m master main

@thedivloop
Copy link

thedivloop commented Jun 24, 2022

It is creating a branch 'master' and that branch I am not able to merge into the 'main' branch. (It show nothing in common so cannot merge)
Sorry if it is a silly question, but I am trying this thing and stuck since many days. Any help would be much appreciated.

I guess.. if you provide more information, people here could help you! For example, actual commands and error messages. And how did you create your master branch and main branch. And so on..

Not a silly question, I struggled until finding out that GitHub is now using main by default instead of master.
If locally your HEAD is on master then you can rename the branch so:

git branch -m master main

Now in the init config file you can change the default branch name. Access the global config here:

git config --global --edit

find

[init]
        defaultBranch = master

and replace master by main

[init]
        defaultBranch = main

save and exit and voila!
This was very helpful for me, hope it was of some use for you too!!

@hilfa007
Copy link

hilfa007 commented Jul 1, 2022

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.

Force it: git push --set-upstream origin master --force

Thanks! It worked for me..

@bayesfanboy
Copy link

bayesfanboy commented Jul 25, 2022

I removed the existing .git directory and voila!

@valhuber
Copy link

valhuber commented Aug 1, 2022

Hugely helpful, though sometimes you need

git push origin main

# NOT git push origin master

@hasii2011
Copy link

hasii2011 commented Aug 1, 2022

All bow to wokeness

@ManohariniA
Copy link

ManohariniA commented Aug 3, 2022

git commit -m 'First commit'
hint: core.useBuiltinFSMonitor=true is deprecated;please set core.fsmonitor=true instead
hint: Disable this message with "git config advice.useCoreFSMonitorConfig false"
error: pathspec 'commit'' did not match any file(s) known to git

what does this error mean?

@Sumrender
Copy link

Sumrender commented Aug 9, 2022

Is there any method for me to push the local repository to remote without first creating a remote repository on github.com ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment