Build your own Git server in Linux

Source: Internet
Author: User
Tags git commands

Build your own Git server in Linux

After setting up your own Git server in Linux, I finally finished it for a long time. record it, or I will forget it next time.

Process:

Server

  • Build git directory

    • Git user, git group as repository Management
    • Ssh authorization (remote access without password)
    • Hook (post-receive) automatically deploys code to the website directory
  • Website directory

    • Just get the code ready.

Client

  • Create Development Directory
  • Ssh connection key Generation
  • Git operation...

Let's get started!

First, git source code compilation

Https://www.kernel.org/pub/software/scm/git/

Download the latest git(.gz) to/usr/local

Install

tar -zxf git-2.2.1.tar.gz cd git.2.2.1make prefix=/usr/local/git allmake prefix=/usr/local/git install

Source code compilation is not as goodyum install git-allConvenient, but the version can be updated. I use Alibaba CentOS6.5, and the corresponding git version can only reach 1.7.2.

The self-installed git is not in the system PATH environment and can be modified/etc/profileManually Paste

Vim/etc/profile # Find PATH =/usr/local/php/bin: $ PATH and change this line to PATH =/usr/local/php/bin: /usr/local/git/bin: $ PATH # Save. Exit shell and reconnect.

Git Repository

Groupadd gituseradd git-g gitcd/home/gitmkdir repo. git # name custom cd repo. gitgit init -- bare # generate a bare repository and store chown-R git: git/home/git/repo in addition to the code version information. git

Note that for security considerations on the internet, only git-shell is enabled for git users' ssh connections. The following operations are required for source code installation:

# Modify/etc/passwdvim/etc/passwd # Find the git user settings, for example, git: x: 502: 503:/home/newbmiao: /bin/bash # change the path of the last execution file to git: x: 502: 503:/home/git: /usr/local/git/bin/git-shell # Under the bin directory of the installation package # To enable the git-shell command interaction cp/usr/local/git-2.2.1/contrib /git-shell-commands/home/git/# in this way, you can only use git commands after connecting to the git account through ssh.

Ssh password-free authentication connection

Su git # Switch git ID cd/home/git/ssh-keygen-C 'your @ email.com '-t rsa # generate an rsa key for you. You can press enter to perform the default operation.

The password generated by the client is the same as that generated by the client.
After the key is generated

. Ssh ── id_rsa ── id_rsa.pub # The public key server needs content to verify the connection identity.

On the client, openid_rsa.pubCopy content

Vim/home/git /. ssh/authorized_keys # paste the Public Key generated by the client, save and exit # Then start sshd and git-daemon/etc/init. d/git-daemon restart # git-daemon in the installation directory/usr/local/git/libexec/git-core/git-daemon, copy it directly to/etc/init. d/sshd start

In this way, the git repository on the server is ready.

Client git Development

Try to submit git operations on the client (The author is the git bash of window)

# Enter an empty working directory git init # initialize gitvim test # edit some content to save and exit git add test # add it to git cache git commit-m'init test' # submit changes # add a remote git repository git remote add origin git @ your_host_name: /home/git/repo. gitgit push origin master # This will synchronize to the server

Others need to synchronize

# Clone and push: git clone git @ your_host_name:/home/git/repo. gitcd repovim READMEgit commit-am 'fix for the README file' git push origin master

Code Synchronization (HOOK)
Git is used for central version control.
However, you also want the server to automatically synchronize the code to the website directory after it receives the modification and update to facilitate testing and development.
The following operations can be implemented:

# Assume that the website directory is cd/home/git/repo under/www/web. git/hooksvim post-receive # create a hook # Write the following content to GIT_WORK_TREE =/www/web git checkout-f # Save and exit chown git: git post-receivechmod + x post-receive

In this way, the code will be automatically synchronized to the specified directory when the modifications are submitted next time.

However, at the beginning, the author still encountered a problem that could not be solved, that is, the ssh Public Key was handed over to the server and startedgit-daemonAndsshdAfter, the clientgit cloneThe password is required, and the prompt is displayed after the password is entered.Permission denied, please try again.

The back end is strange. It may be the permission change, but I don't know why, especially how ssh password-free connection process

I am told that the principle has not been understood yet.

GitHub Tutorials:

GitHub tutorials

Git tag management details

Git branch management

Git remote repository details

Git local Repository (Repository) Details

Git server setup and Client installation

Git Overview

Share practical GitHub tutorials

How to Build and use Git servers in Ubuntu

Git details: click here
Git: click here

This article permanently updates the link address:

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.