Teach you how to build a Git server on Ubuntu

Source: Internet
Author: User
Tags hosting using git git clone

Recently in writing some scripts, in order to maintain continuity, at home also time to catch up with the progress, so encountered the code synchronization problem, currently think of the method has three:

Using the cloud disk, this is used, but because the cloud disk also stores some other things, always real-time synchronization is not appropriate;
Using SVN, the traditional code-hosting tool, has been in use;
Using Git, the latest distributed code hosting tool is said to be powerful.

The final decision to use Git is primarily to learn the latest technology. Here are the steps to make a record, but also to other students to do a guide:

First, the environment:

Service side: Ubuntu 16.04 x64
Client: Windows7 x64

Server-side configuration

To do this, first use the following command to cut root permissions:

sudo bash

When prompted to enter a password, enter the root password.

After the login is successful, start installing git and command:

apt-get install git

When prompted to continue, enter Y to return, and the installation process continues until the installation is complete.

Then start installing SSH, command:

apt-get install openssh-server openssh-client

Also when asked whether to continue, enter Y and return, the installation will be completed automatically.

Then we create a new git-specific user, the user name is also called git, command:

adduser git

After the new success will prompt to set the user password, please set a password you can remember to continue, follow-up details can be filled in as appropriate.

The following is the beginning of the new Git repository, we selected the warehouse directory for/SRV, warehouse name Myfiles.git, so the command:

git init --bare /srv/myfiles.git

Because the current user is root, in order for the GIT dedicated account to be able to manipulate the repository directory, we need to authorize the repository directory to git, command:

chown -R git:git /srv/myfiles.git/
Client actions

First you need to download the Windows version of git: click to download

Click Install after the download is complete and follow the prompts to click "Next" until the installation is complete.

Create a working directory on the client, such as my Gitdir, right-click within the working directory and tap "git Bash here".

In the popup command after the window clone warehouse to Local:

$ git clone [email protected]:/srv/myfiles.gitCloning into ‘myfiles‘...The authenticity of host ‘192.168.252.128 (192.168.252.128)‘ can‘t be established.ECDSA key fingerprint is SHA256:zqtjAg+FGfWrT3SCp1Qa2KqhE2UOy3PmudhhrTFlm7A.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘192.168.252.128‘ (ECDSA) to the list of known hosts.[email protected]‘s password:warning: You appear to have cloned an empty repository.

Note Replace the "192.168.252.128" with your own server IP, and enter the "yes" when you confirm, and finally enter the password to create the git user.

For the client to perform subsequent commits, we also need to specify the current machine user information, command after the following:

git config --global user.email "[email protected]"git config --global user.name "Your Name"

After registering, commit will use this registration information to record the operator information, and then in use git log can see the corresponding information, the effect such as:

$ git logcommit ae72bcc89ea8f5d9d3a44f0e00cf35e91a1afce8 (HEAD -> master, origin/master)Author: sylan215 <[email protected]>Date:   Wed Oct 18 18:37:41 2017 +0800    测试提交

At this point, we complete the entire configuration process.

Files are modified and synced to a Git server

After the configuration is complete, we go into the actual use of the link.

First we modify several files and copy them to the Myfiles directory, then commit to the server and run the commit command under Myfiles:

git add .git commit -am "测试提交"git push

command-line content with output:

$ git add .$ git commit -am "测试提交"[master (root-commit) ae72bcc] 测试提交 1 file changed, 1 insertion(+) create mode 100644 test.txt$ git push[email protected]‘s password:Counting objects: 3, done.Writing objects: 100% (3/3), 223 bytes | 223.00 KiB/s, done.Total 3 (delta 0), reused 0 (delta 0)To 192.168.252.128:/srv/myfiles.git * [new branch]      master -> master

Prompt for password, or enter the password for the GIT account.

Description: For a detailed command of GIT operations, please refer to this article

Upon successful submission, we use the command to synchronize the latest content on another machine git pull :

$ git pull[email protected]‘s password:remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From 192.168.252.128:/srv/myfiles   ae72bcc..afad897  master     -> origin/masterUpdating ae72bcc..afad897Fast-forward test.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)

For complex operations with multiple users, please refer to this article

Other shell that configures the GIT account to be disabled

For security reasons, if you need to disable the shell for a git account, you can modify the/etc/passwd file:

Put one of the

git:x:1001:1001:git-user,,,:/home/git:/bin/bash

Revision changed to

git:x:1001:1001:git-user,,,:/home/git:/usr/bin/git-shell

/usr/bin/git-shellthe path can be obtained by command which git-shell .

Use public keys and private keys for a watertight effect

We need to enter a git user's password to interact with the server each time we use it, so we can configure the private key to be free.

First, you need to generate a public-private key on the client:

ssh-keygen -t rsa

Enter the password after entering the private key, if you want to avoid the secret directly enter, otherwise, customize a password (if you have customized the password, each push and pull is filled with the password set).

After the command executes successfully, the. SSH folder in the current user directory (Windows directory is x:\users{username}.ssh, Linux is/home/{username}/.ssh) generates files "id_rsa" and "id_rsa.pub ", where the. pub file is the public key and the other is the private key.

Copy the file "id_rsa.pub" to the server and set it up with the following command:

mkdir /home/git/.sshcp /home/currentuser/Desktop/id_rsa.pub /home/git/.ssh/authorized_keyschown -R git:git /home/git/.ssh

Note that if the Authorized_keys file does not exist, you can use the CP command, otherwise use the Cat command, for example:

cat /home/currentuser/Desktop/id_rsa.pub >> /home/git/.ssh/authorized_keys

To ensure that the configuration is in effect, you also need to see if the following settings are turned on in the/etc/ssh/sshd_config file:

AuthorizedKeysFile %h/.ssh/authorized_keys

is commented out, if it is, you need to remove the previous # and restart the SSH Service (command service SSH restart).

When all is configured, let's try the effect:

$ git pullAlready up-to-date.

See, this time there is no prompt to enter the password, the free-secret setting takes effect.

22-Port connection to Git

In the. SSH configuration directory, the config file now contains:

host ip地址port 端口名

Config config file in Windows and MAC system location is: X:/users/username/.ssh directory, where X is the system disk, username is the current login user name;

In the case of the Liunx series system, the location is the/home/username/.ssh directory, where username is the currently logged in user name.

Teach you how to build a Git server on Ubuntu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.