First, we need to analyze the problem. To implement a Git remote warehouse server, we need to know:
- How to configure Git server side.
- How to connect to a remote warehouse.
- How to push, pull, fetch and other operations from the remote warehouse.
After the analysis, it is found that it is quite simple. Let's go step by step. The first is the configuration.
preparation
As it is for personal use, it is good to use SSH protocol to implement it. But before that, we have some preparations to do.
First, we create an account named git on the server (that is, raspberry pie) to be used exclusively as a git remote warehouse, and create a. ssh directory and modify its permission to 700. In. ssh, use the touch command to create a new blank file named authorized_keys and modify its permission to 600. The commands are as follows:
$ sudo adduser git $ su git $ mkdir .ssh && chmod 700 .ssh $ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
Authorized here_ The keys blank file will be useful later.
Then we go back to the client. At this time, we need to get a bare repository, that is, the warehouse that does not contain the current working directory.
We first create a new directory and then turn it into a git repository.
# Create a directory called gitproject $ mkdir gitproject # Switch the working directory to gitproject $ cd gitproject # Initialize it as a git repository $ git init # Return to parent directory $ cd ..
Then we clone the warehouse by adding the -- bare option to get the new bare repository we need:
$ git clone --bare gitproject gitproject.git Cloning into bare repository 'gitproject'... done.
You can see the prompt that we clone the bare repository. Next, we need to put the bare repository on the server, that is, we need to start configuring the server. We use scp to create the bare repository git project Put git on the raspberry Pie Server. The complete command is as follows:
$ scp -r my_project.git user@git.example.com:/srv/git
If we use other devices to access the server, we can clone the bare repository. The next step is how to connect the warehouse, but we still have some related preparations to do before that.
At this time, it will be found that there will be problems with push and other operations except clone. This is because the Git server is authenticated through ssh public key. At this time, we need to provide it with public key. By default, the user's ssh key is stored in / ssh directory, so we enter the following commands to check whether the public key has been generated:
$ cd ~/.ssh $ ls id_rsa id_rsa.pub known_hosts
If Id is displayed_ RSA and id_rsa.pub, then the key has been generated. (the suffix of pub is the public key and the other is the private key). If these files are not displayed, use SSH keygen to create them (SSH keygen is provided with SSH package in macOS and Linux). The command is as follows:
$ ssh-keygen -o Generating public/private rsa key pair. Enter file in which to save the key (/home/username/.ssh/id_rsa): Created directory '/home/username/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/schacon/.ssh/id_rsa. Your public key has been saved in /home/schacon/.ssh/id_rsa.pub. The key fingerprint is: # Here is the fingerprint of the key, and finally the Git user name
We can use a text editor (vim, nano, etc.) or commands such as less and cat to view ~ /. ssh/id_rsa.pub to view the public key.
Configure raspberry pie
Just now we have finished the directory, files and keys we need to prepare.
However, since we are the administrator ourselves, we need to copy the public key and add it to the authorized_keys blank file of user git on the raspberry pie we created before on the server side (that is, raspberry pie). Here, we can copy and paste it with a text editor or add it to the end of the file with the cat command.
One might wonder how to copy and paste text from the client into raspberry pie. We can use ssh connection to remotely operate raspberry pie. For details, please see: https://blog.csdn.net/qq_33919450/article/details/122018257
At this time, we can build an empty warehouse for us on the raspberry pie for later use. Here, to initialize the warehouse with git, you need to use git init --bare instead of GIT init, because the -- bare option allows you not to create a working directory during initialization. The specific commands are as follows:
# Because git users only use it as a remote warehouse and will not import other program files, we operate directly in the user's git directory rather than in the srv directory # Create a new project named project Git directory $ mkdir project.git # Switch to project Git directory $ cd project.git # Initialize the directory with the -- bare option $ git init --bare Initialized empty Git repository in /srv/git/project.git/
When we return to the local machine, we can add our local Git warehouse to the remote warehouse on raspberry pie. The specific commands are as follows (please refer to my other blog for the remote warehouse address to obtain and explain below) https://blog.csdn.net/qq_33919450/article/details/122018257 "Get IP for raspberry pie" section in):
# Switch to the directory of the project we need to add to the remote warehouse $ cd gitproject # If you want to create an empty directory, create a new directory, add and commit, and then execute a command # Add the remote warehouse prepared on the raspberry pie. The origin here will refer to the address of the remote warehouse in the future, which can also be understood as referring to the remote warehouse $ git remote add origin git@xxx.xxx.xxx.xxx:gitproject.git # push local warehouse to remote warehouse origin git@xxx.xxx.xxx.xxx :gitproject. master branch of GIT) $ git push origin master
At this time, the remote Git warehouse is configured.
How to operate
Then we can use it like GitHub, such as clone, push, fetch, pull, etc.
Here are some common commands, such as trace, submit, restore, etc. for more detailed commands, please see my other blog https://blog.csdn.net/qq_33919450/article/details/122020108:
Add remote warehouse
git remote add origin git@xxx.xxx.xxx.xxx:gitproject.git
Rename remote warehouse
git remote rename old_name new_name
View remote warehouse
git remote show origin
Remove remote warehouse
git remote remove origin
After modifying or adding files, track them
git add .
Submit update
git commit . -m 'this is comment'
View the status of files in the current warehouse
git status
View the status of files in the current warehouse in more detail
$ git diff
Push updates to remote warehouse
git push origin master
View submission records
git log