Git is a Version Control System. GitHub offer code hosting services based on Git. Check this article to create your own free git repository and push the code into GitHub. GitHub’s free tier doesn’t offer private hosting of the code. There is a monthly fee of $7 to host 5 private repositories, and the expenses go up with more repositories. So, if you think to setup a private Git Server, then this article is for you. This gives you more control over the server/code in it.
Setting up the private Git Server
The first step is installing git on your private server.
yum install git # On CentOS servers or sudo apt-get install git # On Ubuntu/Debian Systems
Next, we have to create a new user on the server so that we can host our repositories under that particular user account.
sudo useradd git # Add user "git" passwd git # Setting password for "git" user
Now, switch to the newly created user account and create an empty repository using the following command.
su - git # Switch to "git" user git init --bare new_project.git # Create an empty Git repository
This will create an empty git repository under “/home/git/new_project.git”.
Now, create a new “.ssh/authorized_keys” file under git user account and add the SSH public key of your local machine (or your developer’s key) to that authorized_keys file. By this way, you can actually SSH to that git user account without a password.
mkdir .ssh chmod 700 .ssh touch .ssh/authorized_keys chmod 600 .ssh/authorized_keys
And use your favorite editor to add the SSH public to the “.ssh/authorized_keys” file. Here, I am using vi editor to do this.
To insert anything into this file, you first need to type i. Now, paste your local public key (or your developer’s key) as a new line into this file and save it by typing ESC and then :wq
That’s it. We have done with the server-side configurations.
Local Repository Configurations
Next, you need to set the remote url (The SSH URL of your server-side Git repository) on the local git repository. So, cd (Change Directory) to your local git repository and execute the following command.
cd /path/to/your_local_repositry/ git remote add origin ssh://[email protected]_server_hostname:/home/git/new_project.git
Now, you will be able to push your contents from the local repository to your remote server-side repository using the following command.
git push origin master
Also, if you want to clone the remote repository to any other system, you can do that using the following command.
git clone [email protected]_server_hostname:/home/git/new_project.git
Always, make sure that you have a proper connection between your local system and the private Git server. Otherwise, you will face issues while performing git operations.
That’s it. We will discuss more git operations on future posts. If you have any questions, please leave a comment.