Git basic tutorial


Basic concepts

In fact, all version control systems can only track the changes of text files, such as TXT files, web pages, all program codes, etc. Git is no exception. The version control system can tell you every change. For example, add a word "Linux" in line 5 and delete a word "Windows" in line 8. Although the binary files such as pictures and videos can also be managed by the version control system, they can't track the changes of files. They can only string up the changes of binary files every time, that is, they only know that the picture has changed from 100KB to 120KB, but the version control system doesn't know or know what has changed.

basic operation

  • Create version Library
git init

It tells you that it is an empty Git repository, and there is one more in the current directory Git directory, which is used by git to track and manage the version library

Step 1: use the command git add to tell Git to add the file to the warehouse

git add readme.txt

Step 2: use the command git commit to tell Git to submit the file to the warehouse

git commit -m "wrote a readme file"

Briefly explain the git commit command, - m followed by the description of this submission. You can enter any content. Of course, it is better to be meaningful, so that you can easily find the change record from the historical record.

If you don't want to enter -m "xxx", will you? There are ways to do this, but it is strongly not recommended, because input instructions are important to yourself and others. If you really don't want to enter the description of children's shoes, please Google by yourself. I won't tell you this parameter.

After the git commit command is successfully executed, it will tell you that 1 file changed: 1 file has been changed (readme.txt file we added); 2 inserts: two lines are inserted (readme.txt has two lines).

Why does Git need to add and commit files in two steps? Because commit can submit many files at once, you can add different files multiple times, such as:

git add file1.txt
git add file2.txt file3.txt
git commit -m "add 3 files."
  • Add remote library
git remote add origin

origin is the name of the remote library, which is Git's default name. It can also be changed to another name

  • View remote quantity
git remote -v
  • Delete one of remote
git remote remove <name>
git remote rm <name>
  • Push all the contents of the local library to the remote library
git push -u origin master
Push the contents of the local library to the remote, using git push Command is actually the current branch master Push to remote.

Because the remote library is empty, we push it for the first time master When branching, add-u Parameters, Git Not only will the local master Remote branch content push master Branches, and local master Branch and remote master Branches are associated, and commands can be simplified when pushing or pulling in the future

You can use the following command later:

git push origin master

Clone, modify, and update projects from github/gitee

Local initialization

git init

Clone the project from github/gitee or server

git clone url

View the project status before modifying the project

git status

Submit the file modification to the local temporary storage area. The command is git add file. File is the modified file name

#To add read MD as an example
$ git add

#Or add them all
$ git add -A

Note: each modified file must be added to the local staging area before it can be updated to the project

Submit the modified contents of the current workspace. The command is git commit -m "modify". The submitted information is in quotation marks. You can fill in other contents yourself

If you need to update the project next time, you can directly git pull, because the channel between the local space and the server or github has been established through git clone

Test whether the configuration is successful

ssh -T
ssh -T

Generate key

Solve the problem that the same computer generates two or more ssh keys and public keys to map two or more GitHub accounts

Use SSH keygen to generate two groups of secret key pairs of different mailboxes

ssh-keygen -t rsa -C "" -f ~/.ssh/id_rsa_self

ssh-keygen -t rsa -C "" -f ~/.ssh/id_rsa_work

Add the key to SSH agent through SSH add

ssh-add ~/.ssh/id_rsa_self

ssh-add ~/.ssh/id_rsa_work

Use the config method to solve the problem of configuring two gitee accounts on one computer

Configure SSH Keys for multiple users

Reference link

  1. Generate two sets of secret keys
    Yes ssh generates two sets of secret keys and sets a config file

  2. Making batch files
    Yes Set two in ssh/bat Bat batch file

The directory structure is roughly like this

│  config
│  id_rsa_self
│  id_rsa_work
│  known_hosts

self version

Script with self as an example
self.config contents are as follows

# Personal gitee public key
PreferredAuthentications publickey
# Specify a specific ssh private key file
IdentityFile ~/.ssh/id_rsa_self

The file is divided into multiple user configurations. Each user configuration contains the following configuration items:

  • Host: the alias of the warehouse website. Take it at will
  • HostName: domain name of the warehouse website (PS: IP address should also be OK)
  • IdentityFile: the absolute path of the private key

self.bat contents are as follows

copy /d %~dp0\self.config /d %~dp0..\config /Y

work version

# Personal gitee public key
PreferredAuthentications publickey
# Specify a specific ssh private key file
IdentityFile ~/.ssh/id_rsa_work
copy /d %~dp0\work.config /d %~dp0..\config /Y
  1. Handle Add the directory where ssh\bat is located to the path of windows
  2. Use the cmd command line to open. When a work warehouse is required, enter the "work" command inside the cmd command line. When you need to switch back to personal, just enter the "self" command on the cmd command line

Configure user names and mailboxes for multiple users

Generally speaking, after installing git, we will configure a global config information, like this:

git config --global "wangshun" // Configure global user names, such as those registered on Github
git config --global "" // Configure global mailboxes, such as those configured on Github

Before formal configuration, we must clear the global configuration (if you have configured it) and execute the command:

git config --global --unset
git config --global --unset

git configuration is divided into three levels: System - > Global - > Local. System is the system level. Global is the configured global, Local is the warehouse level, and the priority is Local > Global > system.

Because we did not configure the user name for the warehouse and cleared the global user name at the beginning, the System level user name, that is, your System host name, will be used if you submit at this time.

Therefore, we need to configure user name information for each warehouse separately. Suppose we want to configure a warehouse of github. After entering the warehouse, execute the following steps:

git config --local "wangshun"
git config --local ""

After execution, view all configuration information of the warehouse through the following commands:

git config --local --list

Added by frog_ on Thu, 06 Jan 2022 01:23:15 +0200