Credit: This post is heavily relied on two articles. 1. How To Set Up A Secure Git Server At Home (OSX) written by Tom Dalling. The above post addresses the scenario, where you want to access your private GIT server over internet. 2. Creating an SSH key on Linux & Mac OS X
The purpose of this post is to focus on the steps that is needed to set up and access a local GIT server.
I am taking the liberty to re-use the original description and screenshots from the original articles wherever applicable.
If you work for a big company, you don’t have to worry much. You will have Atlassian Stash or GitHub who will take care of the source control mechanism for you.
What if you are a sole developer in your company and you don’t want to spend a lot in third party managed source control set up?
You set up your own GIT server. This post is about setting up a GIT server locally.
In our case, there are two developers with MacBook Pros and an extra iMac.
So we decided to set up GIT server in the extra iMac we had.
These were the steps we followed.
1. Add a GIT user account in Server machine.
2. Turn on remote login for GIT user in Server machine.
3. Creating SSH keys in client machines
4. Adding up client’s public keys in Server
5. Making a bare repo in the server
6. Using the new GIT server!
Now the above steps in detail:
For the brevity of the post, assume that you are setting up the GIT server on an iMac.
Also it is assumed that you have an account with admin access in this iMac.
1. Add a GIT user account in Server machine.
Login to the admin account in the iMac.
Open up System Preferences » Accounts and add a standard user
echo 'export PATH="$PATH:/usr/local/git/bin/"' >> ~/.bashrc
2.Turn on remote login for GIT user in Server machine.
Log out of the git user, and log back into your administrator account.
Open up System Preferences » Sharing and turn on Remote Login (this is ssh).
Set Allow access for to Only these users, and add the git user to the list.
3.Set up SSH in client machines
Open a terminal on your local computer and enter the following:
ssh-keygen -t rsa -C "firstname.lastname@example.org"
Associating the key with your email address helps you to identify the key later on.
You’ll see a response similar to this:
Just press <Enter> to accept the default location and file name. If the .ssh directory doesn’t exist, the system creates one for you.
Enter, and re-enter, a passphrase when prompted.
The whole interaction will look similar to this:
You are done.
4.Setting up client’s public keys in Server
You can use the below command to add the public key of the client machine added to the authorized_keys file in the server.
Log into the git user in the iMac
Run this command in the client machine:
cat ~/.ssh/id_rsa.pub | ssh git@MachineName "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
If you have more client machines, repeat the above steps in all of them. You may see some prompts similar to the screenshot below in step 6. Select yes and proceed.
5.Making a bare repo in the server
This is the definition of a “bare” git repo: a repository that can never have files checked out. Everyone pulls and pushes from the server like normal, but nobody can actually work on the server (unless they make a non-bare clone).
If you are creating a new repository called “myrepo.git”, you can make it bare like so:
git init –bare myrepo.git
6.Using the new GIT server
In the client machine all you have to do is the usual clone.
eg: git clone ssh://iMacName/Users/git/myrepo
You may get a prompt like this in the client machine. Opt for YES and enter the password.
You should have the cloned copy in the local machine by this time.
Where to go from here?
In my case, the project was already there in my local machine. I wanted to have the project data centralized in the new git server.
Here are the steps along with the commands that I followed:
|1||Connect to the remote server||ssh git@yourGITMachineName|
|2||Go to the repo folder||cd Documents/repositories/|
|3||Create the sub folder under main repo directory||mkdir myNewProjectFolder|
|4||Create a bare repo in server on the created folder||git init –bare|
|5||Clone the new repo to local machine||git clone ssh://git@yourGITMachineName/Users/git/Documents/repositories/myNewProjectFolder|
|6||Checkout as a new branch||The default branch of the repo is ‘master’. Checkout as ‘develop’. More concepts here.|
|7||Make necessary changes like adding project files to the cloned repo folder||git add .|
|8||Commit the changes||git commit -m “First Commit”|
|9||Push the changes to server||git push origin master|