Skip to content

Instantly share code, notes, and snippets.

@alexpchin
Created June 1, 2014 20:14
Star You must be signed in to star a gist
Save alexpchin/102854243cd066f8b88e to your computer and use it in GitHub Desktop.
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

Copy link

ghost commented Oct 11, 2021

Thank you

@BegenchO
Copy link

Thank you. Worked perfectly. First time user

@dinhtona
Copy link

Thank you

@ZihadHosan
Copy link

Thank you so much!

@semargub
Copy link

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

thanks you so much!

@cameronmcnz
Copy link

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

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

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

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

All bow to wokeness

@ManohariniA
Copy link

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

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

@thedivloop
Copy link

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

In order to push a local repo to a remote repo you need to create the remote repo first. However, if you wanna do it from the command line then you can use the github command line. Check it out here: https://cli.github.com/manual/gh_repo_create.

For a more advanced understanding of the different options (which include bash scripts and change of .gitconfig) this seems very useful: https://stackoverflow.com/questions/2423777/is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-br

@comminco
Copy link

If you do not work this way
check your branch name or your 'README.md' file or use 'git push origin +[branch name]'

@Amirho3einAbbasi
Copy link

Hi guys , in the terminal when i push my project i got this error " fatal: unable to access 'https://github.com/SoliArt-Amirhosein/NFT-ERC721.git/': Operation timed out after 300042 milliseconds with 0 out of 0 bytes received
" can somebody help me to fix this ?

@sararoma95
Copy link

thanks man

@SidneyBasa
Copy link

I searched google and it brought me here to this document. It was one of the top 5 results.
Thank you for making this.

@madelineakers
Copy link

super helpful, thank you sir!

@QuocNam1998
Copy link

its so helpful, thanks you so much

@talham
Copy link

talham commented Aug 10, 2023

Excellent. Thanks @thedivloop and @alexpchin for sharing

@adamdahan
Copy link

Worked great. Thanks!

@IndigoW0lf
Copy link

Thanks so much! Github used to have the screen that popped up after you made a new repo that gave you the instructions to do this (along with other ways to populate your repo), but I didn't see it this time and drew a blank!

@Whevalmeze
Copy link

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?

try using double quotes: git commit -m "First commit"

@craigphicks
Copy link

😆

@heliniApps
Copy link

Thank you for the commands and steps.

Just sharing another way I used to connect my locally initialised 'empty repository' to the already existing remote Git Repo.
My remote repository already had files in it.
I could also clone the remote repository, but just wanted to try pulling the remote changes, instead of pushing the local files first.

To connect the local repo to the existing Git Repository and pull down changes:

  • git init -b main
  • git remote add origin <REMOTE_URL>
  • Check if it's setup correctly using git remote -v
  • git fetch
  • git pull <repository> <branch>
    • e.g.: git pull origin main
    • This will pull the remote "main" branch (origin/main) changes/files, and create the 'main' branch locally.
  • Use git branch command to make sure the "main" branch is created locally.

To push any new local changes (after pulling the remote changes as described above):
  • Make changes to the files.
  • git add <file>
  • git commit
  • git push -u origin <branch>
    • e.g.: git push -u origin main

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