It takes about 5 minutes to read this article.
Recently I was writing some scripts. In order to maintain continuity, I took time to catch up at home, so I encountered the problem of code synchronization. At that time, I thought of three methods:
Use a cloud disk, which is ready to use, but because the cloud disk also stores some other things, it is not appropriate to always synchronize in real time;
Using SVN, a traditional code hosting tool, has been in use;
Using Git, the latest distributed code hosting tool, is said to be very powerful.
Finally decided to use Git, mainly to learn the latest technology. The following are the steps to take notes, but also to guide other students:
Let me talk about the environment first:
Server: Ubuntu 16.04 x64
Client: Windows7 x64
First use the following command to switch to root privileges for operation:
sudo bash
When prompted for a password, just enter the root password.
After the login is successful, start to install git, command:
apt-get install git
When prompted whether to continue, enter y and press Enter to continue the installation process until the installation is complete.
Then start to install ssh, command:
apt-get install openssh-server openssh-client
Also when asked whether to continue, type y and press Enter, and the installation will be completed automatically.
Then we create a dedicated git user, the user name is also called git, the command:
adduser git
After the creation is successful, you will be prompted to set a user password. Please set a password that you can remember and continue. The subsequent detailed information can be filled in as appropriate.
Let's start to create a new git warehouse. We choose the warehouse storage directory to be under /srv and the warehouse name is myfiles.git, so the command:
git init --bare /srv/myfiles.git
Because the current user is root, in order to allow the dedicated git account to operate the warehouse directory, we need to authorize the warehouse directory to git, the command:
chown -R git:git /srv/myfiles.git/
First, you need to download the Windows version of git, download address: https://git-scm.com/download/win
After the download is complete, click Install, and click "Next" according to the prompts until the installation is complete.
Create a working directory on the client. For example, mine is gitdir. Right-click in the working directory and click "Git Bash Here".
After the pop-up command, clone the warehouse to the local:
$ git clone [email protected]:/srv/myfiles.git
Cloning 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 continueconnecting(yes/no)? yes
Warning: 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 that please replace "192.168.252.128" with your own server ip, and enter "yes" when confirming, and finally enter the password when you created the git user.
In order for the client to perform subsequent submission operations, we also need to indicate the user information of the current machine. The command is as follows:
git config --global user.email "[email protected]"git config --global user.name "Your Name"
After registration, this registration information will be used to record the operator's information when committing, and then the corresponding information can be seen when using git log
, the effect is as follows:
$ git log
commit ae72bcc89ea8f5d9d3a44f0e00cf35e91a1afce8(HEAD -> master, origin/master)
Author: sylan215 <[email protected]>
Date: Wed Oct 1818:37:412017+0800 test submission
At this point, we have completed the entire configuration process.
After the configuration is complete, we enter the actual use link.
First, we modify a few files and copy them to the myfiles directory, then submit them to the server, and run the submit command under myfiles:
git add .
git commit -am "Test submission"git push
Command line content with output:
$ git add .
$ git commit -am "Test submission"[master(root-commit) ae72bcc] Test submission 1 file changed,1insertion(+)
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
*[ newbranch] master -> master
The place where you are prompted to enter the password is the password of the git account you entered.
Note: For detailed commands for git operations, please refer to: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
After the submission is successful, we use the command git pull
on another machine to synchronize the latest content:
$ 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/master
Updating ae72bcc..afad897
Fast-forward
test.txt |3++-1 file changed,2insertions(+),1deletion(-)
For complex operations of multiple users, please refer to: http://www.ruanyifeng.com/blog/2014/06/git_remote.html
For security reasons, if you need to disable the shell of the git account, you can modify the /etc/passwd file:
Put in it
git:x:1001:1001:git-user,,,:/home/git:/bin/bash
change into
git:x:1001:1001:git-user,,,:/home/git:/usr/bin/git-shell
The path of /usr/bin/git-shell
can be obtained by the command which git-shell
.
In the above process, we need to enter the password of the git user to interact with the server every time, which will be very troublesome. At this time, we can configure public and private keys to achieve encryption.
First, you need to generate public and private keys on the client:
ssh-keygen -t rsa
After you press Enter, you will be prompted to enter the private key password. If you want to avoid the secret, press Enter directly, otherwise you can customize a password (if you customize the password, you will fill in the set password every time you push and pull).
After the command is executed successfully, the files "id_rsa" and "id_rsa." will be generated in the .ssh folder of the current user directory (Windows directory is X:\Users{username}.ssh, Linux is /home/{username}/.ssh). 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 use the following command to set:
mkdir /home/git/.ssh
cp /home/currentuser/Desktop/id_rsa.pub /home/git/.ssh/authorized_keys
chown -R git:git /home/git/.ssh
Note, if the authorized_keys file does not exist, you can use the cp command, otherwise, use the cat command to append, for example:
cat /home/currentuser/Desktop/id_rsa.pub >>/home/git/.ssh/authorized_keys
In order to ensure that the configuration takes effect, you also need to check whether the following settings in the /etc/ssh/sshd_config file are enabled:
AuthorizedKeysFile %h/.ssh/authorized_keys
Is it commented out? If so, you need to remove the # in front and restart the ssh service (command service ssh restart).
After all configuration is complete, we try the effect:
$ git pull
Already up-to-date.
Look, this time there is no prompt to enter the password, right? The password-free setting takes effect.
The config file in the .ssh configuration directory now contains:
host ip address
port port name
The location of the config configuration file on Windows and Mac systems is: X:/users/username/.ssh directory, where X is the system disk and username is the current login user name;
If it is a liunx series system, the location is the /home/username/.ssh directory, where username is the current login user name.
Recommended Posts