Git tutorial (super detailed)

1. Git download and install

Keep clicking next until the last step is finish.
After the installation is complete, enter

git --version

As shown in the figure below, if the git version number is displayed, the installation is successful.
insert image description here

2. Git configuration SSH key

  1. Enter the following command in the Git Bash window:
git config --global user.name "你的Git账号"
git config --global user.email "你的Git邮箱"

insert image description here
Fill in the mailbox in it as you like, I use my own mailbox.
After that, you can go to your own path to check whether you have generated it (for example, I entered /c/Users/aizi/.ssh/id_rsa), if it is
generated, you don’t need to go to the next step, just skip to the third step.

2. Execute without generating

ssh-keygen -t rsa -C "你的Git邮箱"

Then just enter all the way to avoid entering the password.
As long as there is a strange English matrix, it will be fine.
insert image description here
Generate new SSH keys

  1. Add the SSH public key to the GitLab account.
    Under My Computer—>C Disk—>Users—>Your own user path, you can see a new .ssh folder. There
    is a pair of key files in this folder.
    Use Open id_rsa.pub with notepad++ and other tools, copy the content in it, and do not change the content in it
    insert image description here
  2. Click the avatar in the upper right corner to select edit personal data, then click ssh key, enter the key and add the key.
    Log in to your gitlab account, on the gitlab settings page, find the place to set up SSH
    Click on your avatar
    Click on Settings
    Click on SSH Keys
    insert image description here
    If there is no problem, click the green button
    This is done! ! !

Use of GIT basic commands:

#创建仓库
方法一:git init + 目录名
方法二:手动创建目录名 + git init

#查看仓库状态
git status

#向仓库中提交文件
git add 文件名               --将文件添加到暂存区中
git commit -m "添加描述"     --从暂存区将文件添加到仓库
(可以add多个文件后再一次性commit)
git log                       --查看提交记录
git ls-files                  --列出存储库中的所有文件,包括仅暂存但尚未提交的文件
git reflog                     --获取之前的版本
git checkout  序列号           --返回之前的版本
绿色: 已经加入控制,暂未提交

蓝色: 加入,已提交,有改动

白色: 加入,已提交,无改动

灰色: 版本控制已忽略文件

红色: 未加入版本控制
#常用命令
git pull                                                --刷新git代码库
git branch                                              --查看git的分支 
git branch feature-20230412-13437                       --新建需要的分支
git checkout feature-20230412-13437                     --切换到需要的分支
git status                                              --查看分支中文件的状态
git add protocols/P813/0FBA.json                        --添加文件到暂存区
git add test_msg/0FBA                                   --添加文件到暂存区
git status                                              --再次查看文件的状态,以确认是否添加到暂存区
git commit -m "需求号 需求名 "                           --提交文件并进行备注
git push --set-upstream origin feature-20230412-13437   --上传代码到远程仓库的对应分支

1. Obtain the repository

(1) The function of git init is mainly to initialize an empty warehouse. All subsequent operations under this warehouse will be included in Git version control.

When the Git file is in the modified and staged state, it is not included in the version control of Git, and it is not really included in the version control until it is in the submitted state.

git init

(2) git clone: ​​mainly used to clone a remote repository. That is, copy the corresponding project or repository file in the remote repository to the local, so that the local and remote are consistent.

git clone

2. Version management

(1) git add: mainly to incorporate the currently modified files into the temporary storage area of ​​Git. The files in the temporary storage area will then be included in the Git version warehouse.

git add

(2) git commit: Incorporate the files in the temporary storage area into the Git version warehouse.
Is it possible to roll back the files in the temporary storage area to the modified state?
is allowed.

git commit

(3) git rm: Delete a specific file in the repository.

If a particular file is deleted, it can be retrieved.

git rm

3. View information

(1) git help: Get some help information about Git.

git help

(2) git log: view the commit log

git log

(3) git diff: Compare the differences between the repository files in different states. You can compare the differences between modified and staging files, and between staging and committed files.

git diff

4. Remote collaboration

Involving the concept of remote repository

(1) git pull: it is to pull the files in the remote repository to the local

git pull

(2) git push: It is to push the contents of the local repository to the remote repository.

git push

5. Practical operation

When calling the git init command to initialize an empty warehouse, Git will automatically create a branch. In Git, all our operations must be performed on a certain branch. A project can create any number of branches locally. When git init is called, Git will create a default branch for us. This branch is also called the main branch. When master.master releases a project in a company, it will The code is deployed on the master, and the code is released online.

(1) Create a file named test.txt in the workspace (under the mygit folder).
(2) Add content to the test.txt file, save and close.
(3) Check the status of the workspace

git status

insert image description here
Prompt that the file has not been included in the version library control.
Use git add file name to add files to the temporary storage area.

git add test.txt

insert image description here

git -rm –cached 文件名 
可以将暂存区中的文件恢复到已修改的状态。
git -rm –cached test.txt 

insert image description here

(4) Incorporate the files in the temporary storage area into the repository for management.

Every Git commit must have a comment.
git commit will submit all the changes to the files in the temporary storage area to the Git repository.

git commit

insert image description here
(5) View the submitted history
using the git log command

git log

insert image description here
The commit id of commit, (commit id) is a summary value, which is actually calculated by sha1.

(6) Set user name and email
For user.name and user.email, there are 3 places to set:

a./etc/gitconfig The entire computer range , almost never used
Use the git config – -system command to configure

git config --system

b.~/.gitconfig (very common) for the current user
git config – global

git config --global

c. For a specific project ,
git config – local in the .git/config file
to view the usage of git config: the command is: git config

git config --local user.name 'xyz'
git config --local user.email '[email protected]'

(7) Modify username and email

git config --local user.name 'abc'
git config --local user.email '[email protected]'

(8) Delete a variable

git config --local --unset user.name

(9) The changes before discarding are restored to the original state

git checkout --test.txt
git status

(10) Multiple revisions enter the temporary storage area

git status
git reset HEAD test.txt
git status
git add test.txt
git commit -m 'second commit'
git status

The commit id can be used to locate the commit at a specific historical moment.

Guess you like

Origin blog.csdn.net/weixin_43138792/article/details/130154352