Git server setup

In general, there are two ways to build a local Git server, one is to create an account for each user who uses the Git service, and the other is to create a public account (the account name is usually git) for everyone to use. Generally, the second method is used to avoid unnecessary account addition and read and write permission settings.

1. Server side

Install ssh and git

sudo apt install openssh git

Create and configure a git account

sudo adduser git
sudo chsh -s /usr/bin/git-shell git

The second command changes the git user's shell to git-shell to restrict the git user's login behavior. With the help of a restricted shell tool called git-shellRestricted Shell, you can conveniently limit the activities of the user git to those related to Git.

Modify the owner of the server code storage directory to git

Suppose the code storage directory is /opt/git:

sudo chown -R git:git /opt/git

Create a bare repository, the bare repository directory name .gitends with

  • /opt/gitCreate an empty repository under

    cd /opt/git
    mkdir project.git
    git init --bare
  • Create an empty repository from an existing local repository

    git clone --bare project project.git
    scp -r project.git git@server:/opt/git

2. Local

Install ssh and git

sudo apt install openssh git

After the installation is complete, first configure the username and its email.

Create ssh key and upload to server

ssh-keygen -t rsa -C "[email protected]"
ssh-copy-id -i git@server

email is the email of the git user in the previous step, and the password of the key can be empty. By default, this command generates two files in the ~/.ssh directory id_rsa, id_rsa.pubwhich are the private key and public key of the current user.
The second command copies the public key to the server's git user and saves it in a file ~/.ssh/authorized_keys. Of course, it can also be manually appended to the end of the file, with each public key on a new line.

test

git clone git@server:/opt/git/project.git
cd project
echo README >> README
git add README
git commit -m "add README"
git push origin master

Associate the current warehouse with the remote warehouse

# in project directory
git remote add origin git@server:/opt/git/project.git
git push -u origin master

First add the repository association, and then masterpush the content to the branch. The -uparameters are used in the first push to associate the local branch with the remote branch.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325170049&siteId=291194637