Create a Remote GIT repository and PUSH stuff to it

Create a Remote GIT repository and PUSH stuff to it

Last updated:

If, for one reason or another, you don't want or can't use Github or other similar solutions to host your code but you own a server and have SSH access to it, you can use that server as a git repository.

It's possible to use your remote server as a git repository for your code

Here I'll use the terms server-side and client-side to define where you should do each step:

  • The client-side is the computer you are sending (pushing) code from. In most cases, this will be where you develop code.
  • The server-side is the computer you will send code to. This is probably the server where you will deploy your project.

All commands were run on Ubuntu linux

On the server-side

Download git:

$ sudo apt-get install git-core

create a user called git:

$ sudo useradd git

create a directory for the newly-created user:

$ sudo mkdir /home/git

Create the repository that's going to hold the project:

$ sudo mkdir /home/git/your_repository_name.git

cd to your newly-created repository

$ cd your_repository_name.git
$ sudo git init --bare

On the client-side

cd to the directory where your project is located:

$ cd path/to/your/project

create a GIT repository on it:

$ git init

add the files you want git to track: (some examples:)

$ git add file1.php
$ git add file2.py
$ git add file3.rb
$ git add someDirectory/

commit the added files: you will be prompted to add a commit message.

$ git commit -a

now you want to tell git where your remote repository is located:

$ git remote rm origin

you may see an error message here but it's ok.

$ git remote add origin git@your_server.com:your_repository_name.git
$ git push origin master

note: In order for this to work, you must have a public/private key pair for the user git to log into your remote server, although you could replace it with any other username you want. (look at this if you're not sure how to work with key pairs!) The remote repository your_repository_name.git should be located in /home/git/ on the remote server.