Tuesday, February 14, 2017

How to Push an Existing Repository to Git Server

Say you have a Github account or a local git server where you want to publish your work either publicly or privately as a bare repository. This post will explain how to create a bare repository on Github or git server. Refer here for official Github documentation.

First off, you will need to create a bare repository. If you are using Github, simply create a new repository. Note that you must NOT add README or, .gitignore files.

If you are using a local git server, you will need to run the following as a local user in the server:
$ git init --bare test_repo.git 

Note that by convention bare repositories end with .git to differentiate with working repositories. Bare repositories refer to those that are on Github or a local git server where users only upload / download and not directly work with. In contrast, a working repository is what you would have on your desktop, constantly editing and saving files locally.

Next, open up terminal and change directory to your project directory. If you haven't initialized it as a working git repository, then do so.
$ git init
$ git add .
$ git commit -m "initial commit"

Now, add your remote repository
$ git remote add origin GIT_REMOTE_REPOSITORY

where GIT_REMOTE_REPOSITORY will be your remote git repository in https or ssh.

Finally, push your current repository to the remote repository:
$ git push -u origin master

Now, the two repositories should be in sync!

No comments:

Post a Comment