Git Server Setup

Mina Ayoub
6 min readNov 18, 2016

--

1. Introduction and advantages and disadvantages of Git

What is git?

Git is the most widely used distributed version control system in the world. It was created by Linus Torvalds and was originally designed to better manage Linux kernel development.

What are the advantages and disadvantages of svn?

1. the core difference with svn is that Git is distributed, and svn is non-distributed. This is mainly reflected in the existence of a Local Repository on each developer computer when using Git. This Local Repository contains all the files in the remote library, so you can commit the commit and view the historical version record even when there is no network. Create a project branch and other operations, and then connect the network to the server again.

2. Git stores the content in metadata, while SVN is stored as a file. The performance of .git files is very different from the size of .svn files.

3. SVN has a self-incrementing global version number; and each time Git commits, a SHA-1 hash value is calculated by the content of the file or the structure of the directory, and a 40-digit hexadecimal string is obtained. This is the version number.

4. The integrity of Git’s content is better than SVN: Git’s content storage uses the SHA-1 hash algorithm. Linus, the Linux kernel pioneer and Git developer, said that Git uses SHA-1 not for security, but for data integrity; it guarantees that after many years, when you recheck out a commit It must be the state of its time many years ago, completely the same, completely trustworthy.

5. After Git is downloaded, all Logs can be seen in the OffLine state, and SVN cannot.

6. SVN must first Update to Commit, forgetting to merge will cause some errors, git is still relatively rare.

7. To clone a new project, Git only needs to clone the master branch; svn needs to copy all versions. Git saves time.

8. repository (repository): SVN can only have one specified central repository. When there is a problem with this central repository, all the work members will work together until the repository is repaired or the new repository is set up. Git can have an unlimited number of repositories. Or, to be more correct, every Git is a repository, the difference is whether they have an active directory (Git Working Tree). If something happens to the main repository (for example, the repository that is placed on GitHub), the working member can still commit in their local repository, waiting for the main repository to recover. Work members can also submit to other repositories!

9. SVN create branch will affect other people; Git can create any branch locally, as long as I do not merge and submit to the main repository, it will not affect anyone.

10. Commit In SVN, when you submit your finished product, it will be recorded directly to the central repository. When you find that there is a serious problem with your finished product, you can’t stop things from happening. If the network is interrupted, you can’t submit it at all! Git’s submission is entirely part of the local repository’s activities. And you just need to “git push” to the main repository. Git’s “push” is actually performing “Sync”.

11. the mode will be relatively more complicated than SVN.

2. Git installation

Installation environment: Linux CentOS7

I use yum installation directly, execute the following two lines directly in linux

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel     // install dependencies 
yum install git // install Git can also run yum intall directly git yum will automatically install software dependencies

This way the Git environment is installed, and the old is the permission settings, who can access this Git server?

3. User access settings

3.1. The key login type

1. First create a user group and user

1 groupadd git     // Create git user group 
2 useradd git -g git // Create git user to run git service

2. Collect the public key of the required login user and import the public key into the /home/git/.ssh/authorized_keys file, one per line.

The public key in Linux is in each user’s home directory (/home/user/.ssh). If not, use the command to create it: ssh-keygen -t rsa

Public key creation in Windowns:

Open Git Bash with git.
1. Install git and open “Git Bash” from the program directory.
2. Type the command: ssh-keygen -t rsa -C “suifengin412@gmail.com” //Red font can be used or not.
3. Specify the directory where the file is generated.

Generate two files in the specified directory: id_rsa and id_rsa.pub

Go to the git home directory and add the collected public key (the contents of id_rsa.pub) to the authorized_keys file (one per line).

Created without the authorized_keys file:

cd /home/git/
mkdir . ssh
chmod 700 . ssh
vim .ssh/ authorized_keys
chmod 600 .ssh/authorized_keys

Note: The .ssh directory must be 700 permissions, the authorized_keys file must be 600 permissions.

3.2. Password login type

1. Create a login user and add to the git user group

useradd -M git1         // -M means not creating a home directory 
useradd - M git2
passwd git1  / / set the password
passwd git2
usermod -G git git1 // to add new users to the two groups git
usermod -G git git2

2. Prohibit the newly created user ssh login to the Linux server

which git-shell        // View the path where git-shell is located (my is: /usr/bin/git-shell) 
vim /etc/passwd // username:x:1000:1000::/home/username: /bin/bash Replace /bin/bash with the path where git-shell is located

Note:
When you create a user, add -M. The main purpose is to not generate the home directory of the user with read and write permissions in the home directory. This is because the users we created are for the git service and do not want to give the user other permissions than git access; for the same reason, users are prohibited from logging into the Linux server via ssh.

4. Initialize the git server

We will choose the git directory as the git repository.

chmod g-w /home/git     // this is necessary 
cd /home/git // enter the home directory git
git init --bare test.git // create an empty warehouse, Git repository on the server usually end in .git
chown -R git:git test.git/ // Set permissions, because I use root to create this empty repository, so I need to change the corresponding permissions to set
chmod -R 775 test.git/ // set all permissions of the group, otherwise secret Key type push is unsuccessful

Note: It is important to change the owner of the file and the group permissions, remember.

5. Clone warehouse

git clone git@git_ip:/home/gitrepo/runoob.git    // git_ip is the server ip of Git and needs to be modified to your own Git service ip.

--

--

Mina Ayoub

I'm enthusiastic about being part of something greater than myself and learning from more experienced people every time I meet them.