Setup A Private Git Server on Debian 7

Introduction

This tutorial will show you how to configure a centralized Git server as an alternative to using third-party services, such as Github or Bitbucket. Running your own server is likely to be cheaper and safer. It is "safer" is because large sites, such as Github, are a bigger target for hackers. Also, you know exactly how your server is configured and where your data is stored.

Why Git?

It seems to me that the primary reason to use Git is that everyone else is using it. Thus, it is the most likely tool to be easily integrated into other tools/services as they arise such as composer/satis. The prevalence of git also means that it's very easy to start working with others with one less thing to agree on (people can get very passionate about which tools they use). In terms of version control itself, I think that most people are not investing enough time into studying the alternatives, such as SVN or Mercurial.

Why Debian?

I wanted to use a distro that I wouldn't need to update and reboot every other week, like I do with Ubuntu. I have stopped "supporting" CentOS ever since version 7, and Debian is incredibly easy to work with, coming from a heavy Ubuntu user's perspective.

Server Steps

    Add a git user to the server that will own the repositories. Every time that a developer wants to commit to this centralized repository, they will need to authenticate with this user and it's password. However, it will not be the name that appears on the git logs, that will be the name that the user set with
    git config user.name "Developers Name"
    sudo adduser $GIT_USER
    # fill in the details and use a strong password.
    su $GIT_USER
    cd $HOME
    
    A better system would be to have a user for each developer who is going to work on the project. Thus each developer has their own username and password to authenticate with. This also allows some users to only have read access, rather than write, which is useful for automated deployments. This also makes removing a developer from the project much easier.
    I will show you how to do this in another tutorial and link to it when it becomes available.
    Install Git
    sudo apt-get install git -y
    Optionally, create a folder we are going to store all our repositories inside of. If you created a user just for git in step 1, then I recommend that you don't bother doing this, and just stick all the repos in the git user's $HOME
    mkdir repos
    cd repos
    
    Create a repo
    mkdir $REPO_NAME
    cd $REPO_NAME
    git init --bare
    
    "A bare repository usually serves as a point of coordination for a centralized workflow, in which several people push and pull from that repository rather than directly among themselves; no one works with the bare copy directly." [Git Pocket Guide ]

Client Steps

    Navigate to where we want to store the code and clone the repository
    cd /path/to/stick/repo
    git clone $GIT_USER@$SERVER_IP/path/to/repos/$REPO_NAME
    
    Congratulations! You now have a local copy of the repository.
    Here is an example of how to add a file, commit it locally, and then push all the changes that have occurred to the centralized repository.
    touch README.txt
    git add README
    git commit . -m "My commit description goes here"
    git push $GIT_USER@$SERVER_IP:/path/to/repos/$REPO_NAME
    
    It is a good idea to "push" to the centralized repository frequently to avoid merging issues, but you don't have to perform a "push" after each commit. However, the changes are only available for others to use when they have been pushed.

No comments:

Post a Comment