Create git server
Foreword: When we want to collaborate with several partners to develop the same project, or build a resource sharing platform, Git is a good choice.Of course, since it is a shared platform, it is obviously inappropriate to put this platform on a personal computer, so GIT must be built on the server.Another requirement is that we develop it locally, then push it to the server, and automatically synchronize it to the web site directory, so that we can directly see the effect of the web page, which requires automatic synchronization.Below I will lead you to implement such a function. If conditions permit, you can follow suit (note: my server is centos 7, lamp environment).
1. Install git on the server:
//yum安装
yum install -y gitAfter installation, you can run git to test to see if the installation is successful. 2. Create a bare version library on the server: ps: A remote repository is usually just a bare repository—that is, a repository without a current working directory.Because the repository is just a collaboration medium, there is no need to take a snapshot of the latest version from the hard drive; only Git data is stored in the repository.Simply put, the bare repository is the contents of the .git subdirectory in your working directory Let’s create a bare repository called sample.git under /home/testgit/:
mkdir /home/testgit
cd /home/testgit
git init --bare sample.git
//这里 git init 是初始化空仓库的意思,而参数 --bare 是代表创建裸仓库,这个参数一定记得带上When the last command above is run, there will be a prompt: Initialized empty Git repository in /home/testgit/sample.git/ If you don't get that result, you may want to go back and check which step went wrong. 3. Create the web site directory www If you already have a lamp environment, then I believe you already know this directory. For details on building a lamp environment, you can read my other blog: Building a lamp platform environment in centos 7, how to change the apache default website directory in Centos7 system Now my web site directory is in /home/www Four. Prerequisite: git is installed locally Open git bash, I plan to create a repository named lsgogit under my D drive
cd /d
git clone git@115.159.146.94:/home/testgit/sample.git lsgogitSince I couldn’t download it, I found a solution.
git clone ssh://git@104.160.37.xx:27190/git/mamingjuan/mamingjuan.git
git clone ssh://git@23.105.194.xx:27382/git/tx-lashou/sample.git tx-lashouIf the public key is not configured here, you will be prompted to enter the password, but we may not know the password, so configure the public key: 1. Check your computer’s public key: //Check if there is an ssh key. If there is no key, there will be no such folder.
cd ~/.ssh
ls -al
//如果列出了authorized_keys2 id_dsa known_hosts config id_dsa.pub 则证明你拥有公钥。We need to look for a pair of files named id_dsa or id_rsa, one of which has a .pub extension.The .pub file is your public key and the other is your private key.If you cannot find such files (or there is no .ssh directory at all), you can create them by running the ssh-keygen program.On Linux/Mac systems, ssh-keygen is provided with the SSH package; on Windows, the program is included in the MSysGit package. First ssh-keygen will confirm where the key is stored (default is .ssh/id_rsa), then it will ask you to enter the key password twice.If you don't want to enter a password when using the key, just leave it blank. Users who have done this will now need to send their public keys to any Git server administrator (assuming the server is set up with public key-based SSH authentication).All they have to do is copy the contents of their respective .pub file and send it via email.The public key looks like this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAcnTC3A++ginnxr2dvtQa881abjGIzk99KQVg/sPpqvtT3espFKBs/wqrJ9naIPtCswKDMg2tBVgJzg5FimDjI44O072qAftRcjU2EEySlGDDetREOvcDxqcXzf6cAdhnKlYcPk8s46oC+SxhK0zAFZCZNc7z9GcEPOC+ESIettycDw/Bo11WfxAWS0hky2F3rYh5CRr8j7BKRsAvEaAyKwzmM9X0XCRniC2pD0ObX0/SjPwq9Q5/Vjg03muiSo5Bm/xmuHkwb4/uQglBwnaSqEr6YkL9xLMoNsQaJ1TAvTrEaZLNhWzN8iaPyKZMJEzPLX1NgeEx9AEae39N+NsJ LSGO@zhongjinNow let's go back to being the server administrator Since when installing git earlier, centos will create a git user by default (other systems seem to need to be created manually), we enter /home/git/.ssh, which contains a file named authorized_keys. We add the public keys of other developers (select all, copy, cannot be changed!) to the file. Each developer's public key is divided into one line. Now let's look at the authorized_keys files of two developers (PCs are zhongjin and jinjinzhong):
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAcnTC3A++ginnxr2dvtQa881abjGIzk99KQVg/sPpqvtT3espFKBs/wqrJ9naIPtCswKDMg2tBVgJzg5FimDjI44O072qAftRcjU2EEySlGDDetREOvcDxqcXzf6cAdhnKlYcPk8s46oC+SxhK0zAFZCZNc7z9GcEPOC+ESIettycDw/Bo11WfxAWS0hky2F3rYh5CRr8j7BKRsAvEaAyKwzmM9X0XCRniC2pD0ObX0/SjPwq9Q5/Vjg03muiSo5Bm/xmuHkwb4/uQglBwnaSqEr6YkL9xLMoNsQaJ1TAvTrEaZLNhWzN8iaPyKZMJEzPLX1NgeEx9AEae39N+NsJ LSGO@zhongjin
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAcnTC3A++ginnxr2dvtQa881abjGIzk99KQVg/sPpqvtT3espFKBs/wqrJ9naIPtCswKDMg2tBVgJzg5FimDjI44O072qAftRcjU2EEySlGDDetREOvcDxqcXzf6cAdhnKlYcPk8s46oC+SxhK0zAFZCZNc7z9GcEPOC+ESIettycDw/Bo11WfxAWS0hky2F3rYh5CRr8j7BKRsAvEaAyKwzmM9X0XCRniC2pD0ObX0/SjPwq9Q5/Vjg03muiSo5Bm/xmuHkwb4/uQglBwnaSqEr6YkL9xLMoNsQaJ1TAvTrEaZLNhWzN8iaPyKZMJEzPLX1NgeEx9AEae39N+NsJ LSGO@jinjinzhongAt this point, we can clone the remote warehouse without entering a password:
cd /d
git clone git@115.159.146.94:/home/testgit/sample.git lsgogitAfter twists and turns of verification:
git clone ssh://git@104.160.37.60:27190/git/mamingjuan/mamingjuan.gitThat's how it works
git clone ssh://git@10.1.36.221:22//data/projects/tx-frontend/txls-weex/txls-weex.gitAfter the command is run, lsgogit is now an empty warehouse. Now we can add files under the warehouse and push to the remote warehouse.
cd lsgogit
touch index.php
git add index.php
git commit -m "test commit"
git push origin masterIf the push here fails, it is most likely due to a permissions issue on the server, such as the testgit folder we created before. My solution here is:
chown -R git:git testgitAssign the testgit folder and the subfolders below to git, thus ensuring a successful push. There may be some prompts when you push for the first time, because there is nothing in the bare version library. You may need to write the full command of git push origin master. After that, it is not necessary. You can just git push directly. So far, we have completed the first task and implemented a shared platform that can both pull and push data. 4. Automatically synchronize to the site directory (www) For example, just now we pushed the index.PHP file to the remote warehouse. Although it was prompted that the push was successful, we still cannot see the effect on the server side, which makes us feel unhappy.For example, if I write an HTML page and want to see it immediately on the site, automatic synchronization comes in handy. The automatic synchronization function uses the hook function of git. Server side: Enter the bare warehouse: /home/testgit/sample.git
cd /home/testgit/sample.git
cd hooks
//这里我们创建post-receive文件
vim post-receive
//在该文件里输入以下内容
#!/bin/bash
git --work-tree=/home/www checkout -f
----------------------------------------------------------
git --work-tree=/data/projects/tx-frontend/www checkout -f
//保存退出后,将该文件用户及用户组都设置成git
chown git:git post-receive
//由于该文件其实就是一个shell文件,我们还应该为其设置可执行权限
chmod +x post-receiveNow we can modify the index.php file on the local computer, or add a new file, submit it to the remote warehouse, and then go to /home/www to see if there is the file we just submitted. If you find that the push is successful in the project pushed by Git but there is no code of your own in the www directory, you should pay attention at this time: this is due to the permissions of the folder!Assuming that the user group to which your www directory belongs is root, you can add your git user to this group and add write permissions to git, or other solutions. Anyway, you want the git user on the server to have permission to enter the www folder.
Note: chown git:git -R www/ This way www has read and write permissions for git.At this point, you can implement git submission