git tutorial (covering GitHub\Gitee\Gitlab)

Introduction: Personally compiled git tutorials from Silicon Valley on site B for easy reference;

Table of contents

1、Git

1-1. Introduction to git:

1-1-1. Git overview

1-1-2. Why version control is needed:

1-1-3. Centralized version control and distributed version control:

1-1-4. git working mechanism:

1-1-5, git and code hosting center:

1-2. git installation: (installation explanation)

1-3. git command: (explains common commands)

1-3-1. Command_Set user signature

1-3-2. Detailed explanation of git about local warehouse, version library, workspace, staging area, remote warehouse and branches

1-3-3. Command_Initialize local library

1-3-4. Command_View local library status

1-3-5. Command_Add temporary storage area

1-3-5. Command_Submit local library

1-3-7. Command_modify file

1-3-8. Command_version shuttle

1-4. git branch:

1-4-1. Branch operation command:

1-4-2. View branches, create branches, switch branches, and submit files to new branches:

1-4-3. Merge branches:

1-4-4. Merge branch conflict handling:

1-4-5. Branch push and pull operations

1-5. Idea integrates git

2、GitHub

2-1. Create a remote library

2-1-1. Register a github website account

2-1-2. Create a remote library

2-1-3. Add remote library and create alias

2-2. Code remote push push

2-2-1. Command:

2-3. Code pull pull

2-3-1. Command:

2-4. Code clone clone

2-4-1. Command:

2-4-2. What does the git clone operation do?

2-4-3. What is the difference between git clone, git pull and git fetch:

2-5. SSH password-free login

2-6. Idea integrates github

3. Gitee code cloud

3-1. gitee creates a remote library

3-2. Idea integrates gitee

3-3. gitee connects to github to copy and migrate code

4、GitLab

4-1. Construction and deployment of gitlab server

4-2. Idea integrates gitlab


1、Git

1-1. Introduction to git:

1-1-1. Git overview

It is a free, open source distributed version control system that can handle a variety of projects from small to large quickly and efficiently.

1-1-2. Why version control is needed:

  • Team collaboration development;
  • It can record the history of file modifications, making it easy to view historical versions, version rollbacks, and version modifications;

1-1-3. Centralized version control and distributed version control:

Here we discuss the differences between the centralized version control tool svn and the distributed version control tool git:

  • Git is distributed and has two repositories, local and remote, while svn is centralized and has only one remote repository.
  • Git has a work area, a temporary storage area, and a remote warehouse. git add submits the code to the temporary storage area, git  commit submits it to the local warehouse, and git  push pushes it to the remote repository; svn means add to submit the code to the temporary storage area, and commit means submitting it to Remote repository;
  • Therefore, git solves the shortcomings of centralized version control. 1. Development is carried out when the server is disconnected from the Internet (version control is performed locally); 2. Each client saves a complete project (including historical records, more Safety)
  • git and svn commands

1-1-4. git working mechanism:

Collaborative development:

Collaboration within the team:

Cross-team collaboration:

1-1-5, git and code hosting center:

The code hosting center is a remote code warehouse based on a network server, generally called a remote library.

  • LAN: GitLab
  • Internet: GitHub (external network), Gitee (domestic website)

1-2. git installation: (installation explanation)

See your own blog post link for details:  git download and installation tutorial:

[https://blog.csdn.net/weixin_42640280/article/details/127040860

1-3. git command: (explains common commands)

1-3-1. Command_Set user signature

effect:

The purpose of setting a signature is to distinguish the identities of different operators. The user's signature information can be seen in the submission information of each version, and you can confirm who submitted the version; the user signature must be set for the first installation of Git, otherwise it cannot Submit code;

The git user signature set here is the user name that distinguishes the submitter, and has nothing to do with the accounts registered on github and gitee;

Set up git native user signature

1. There are two ways to set the user signature here, one is to set the project level, and the other is to set the system user level;

2. The priority of the two user signatures is, project\warehouse level > system level. If both are set, the user signature at the project\warehouse level will be taken;

3. Both user signatures cannot be set;

4. Git will not verify whether the email address exists, so you can name it as you like;

Method 1: Project\repository level (valid only within local library scope)

git config user.name 用户名(不能是中文)
git config user.email 邮箱地址

Information storage location: ./.git/config file

Method 2: System level (user logged in to the current operating system, that is, the local machine)

git config --global user.name 用户名(不能用中文)
git config --global user.email 邮箱地址

Information saving location: ​~/.gitconfig ​file

Check whether setting the user signature is successful.
Native operation:

 This machine is configured with a global user signature. To check whether it is successful, open the path: C:\Users\ZCQ\.gitconfig file

全局的git账户签名设置是否成功查看
C:\Users\用户名\.gitconfig文件

直接命令行查看
$ cat ~/.gitconfig
如下:
[user]
        name = zhangchq
        email = [email protected]

1-3-2. Detailed explanation of git about local warehouse, version library, workspace, staging area, remote warehouse and branches

https://blog.csdn.net/weixin_42640280/article/details/127074182

1-3-3. Command_Initialize local library

Description: Execute the command in the directory where you want to create a local library

初始化本地库命令:
git init

After initialization, there are these files in the .git directory

Operation of this machine:

Directory F:\_2_GitSpace\_1_github\git_demo, used to initialize the local library;

 Enter the folder directory where you want to create a new local library and execute the command git init:

Generate hidden folder .git, initialization completed;

1-3-4. Command_View local library status

Order

查看本地库命令:
git status

Status of git workspace files:

Git workspace files contain two statuses: untracked and tracked

Untracked: such as a newly created file in the workspace

Tracked: It can be modified, submitted to temporary storage, or submitted to the local warehouse.

File status:

 Analysis:

untracked

unmodified

modified

staged (staged)

Untracked status:

Modified and uncommitted staging area:

Submit staging area:

 

Submit to local repository:

-----------------------------

The status viewed by git status just after git init initializes the local library:

translate:

On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)

在分支主
还没有提交
无需提交(创建/复制文件并使用“git add”进行跟踪)
  • Prompt which branch the file is on
  • Prompt file status in local warehouse
  • Prompt that the file is in the temporary storage area

For demonstration, add two files to the local library, and then use the command git status to view the status:

1. Add two files without git add to the staging area:

 translate:

$ git status
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        helloworld.c
        helloworld_2.c
nothing added to commit but untracked files present (use "git add" to track)
----------------
在主分支
还没有提交
未跟踪的文件:
   (使用“git add <file>...”来包含将要提交的内容)
         helloworld.c
         helloworld_2.c
没有添加到提交但存在未跟踪的文件(使用“git add”来跟踪)

2. Git add the helloworld.c file to the staging area. The git status after helloworld_2.c is not added to the staging area:

translate:

$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   helloworld.c
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        helloworld_2.c
-------------
在分支主
还没有提交
要提交的更改:
   (使用“git rm --cached <file>...”取消暂存)
         新文件:helloworld.c
未跟踪的文件:
   (使用“git add <file>...”来包含将要提交的内容)
         helloworld_2.c

1-3-5. Command_Add temporary storage area

Order:

添加到暂存区命令:
git add <path>   //添加指定path,这里的path可以是文件,也可以是文件夹
git add .       //添加所有文件
git add -u      //添加新创建的文件,只添加已修改和删除的文件
--------
//如果要撤销提交文件到暂存区,
git rm --cached <path> //撤销暂存指定文件或文件夹
git restore --staged <file>  //取消暂存文件

Operation of this machine:

The warning prompts that the newline character CRLF will automatically switch to LF. Don't worry about it. Operate as follows. The file helloworld.c prompts the status of the temporary storage area from red to green, and prompts a new file, that is, helloworld has been added to the temporary storage area;

1-3-5. Command_Submit local library

Order:

git commit -m "版本日志" <文件名> //将暂存区中的文件提交到本地仓库
------
git reflog     //查看提交记录,(可以查看到所有分支的所有操作记录信息,方便方便回退版本)
git log        //查看提交记录(比较详细,完整版本号,提交人等信息)

Operation of this machine:

Check the status. Because helloworld_2.c was deliberately not submitted to the staging area, it prompted that a file was not tracked.

View submission information:

 Note:

1. As shown in the figure, 4e289d2 is the version number, and the value after commit is the complete version number.

2. The biggest difference between git log and git reflog is whether you can query deleted commit records and reset operation records. Log cannot, but reflog can;

1-3-7. Command_modify file

Operation: Modify the files in the workspace ---> Check and you will be prompted that there are files that have been modified but not tracked----》Submit the file to the temporary storage area----》Submit the temporary storage area file to the local library---》Repeat View submission information

1-3-8. Command_version shuttle

Order


命令:
git reset --hard  <版本号>  //硬回退
git reset --soft <版本号>   //软回退

具体步骤:
1、git reflog   //查看历史提交信息,选择要回退的版本号
2、git reset 命令回退到指定的版本

总结:
1、git reset --hard 会覆盖到本地工作区的代码,git reset --soft不会覆盖本地工作区代码;

Soft rollback, code content is not covered:

Note: After rolling back git reflog, you can see the rollback operation in the repository, but git log cannot.

After the soft rollback, the operation results are re-submitted from the staging area to the local warehouse:

Summary: As long as the version is submitted to the local repository (git commit), the version is recorded. You can use git reflog to see the history of all submissions and rollbacks. No matter how many versions you roll back, you can freely switch the version of the code;

1-4. git branch:

1-4-1. Branch operation command:

git branch -v        //查看分支
git branch <分支名>   //创建分支
git checkout <分支名> //切换分支
git merge    <分支名> //把指定的分支合并到当前分支上
-------
git branch (-m/-M)  <旧分支名>  <新分支名>   //重命名分支(新分支名要是重复,用M强制重命名)
git branch (-d/-D)  <分支名>  //删除分支

=========分支的push与pull操作
git branch -a      //查看本地和远程分支
git push <远程库名> <远程分支名>  //推送分支到远程(和推送代码一样命令)
git push <远程库名> :<远程分支名>     //删除远程分支(本地分支保留)(这个要谨慎操作,就差一个冒号)
git checkout -b <本地分支名> <远程库名/远程分支名> //拉取远程指定分支并在本地创建分支

1-4-2. View branches, create branches, switch branches, and submit files to new branches:

Same submission process:

1-4-3. Merge branches:

As shown below, the contents of helloworld.c in the leaf01 branch are merged into helloworld.c in the master branch.

 

1-4-4. Merge branch conflict handling:

  • Cause of conflict: When merging branches, the two branches have two completely different sets of modifications at the same location in the same file . Git cannot decide which one to use for us, it must be decided manually ;

Currently, the changes you see are local files, so you need to submit them to the local library of the master branch, then modify different content in the same place, submit it to the local library of the leaf01 branch, and then merge the branches, and conflicts will occur;

The eighth line of helloworld.c on the master branch:

The content of the eighth line in the helloworld.c file in the leaf01 branch:

Tips for branch merge conflicts:

 Merge conflict resolution:

Modify the conflicting files according to actual requirements, and then submit them to the staging area and local warehouse; 

Pay attention to a pitfall:

As shown in the figure below, the (master|MERGING) status is represented in the merge branch, so the command format to submit the local library after modifying the conflict file is: git commit -m "version information". There is no need to specify a file name, otherwise the following prompt will appear , and submitting the local library failed: 

The submission is successful, that is, the merge of branch leaf01 to the main branch master is successful.

==================================

Problem with switching branches:

  • Question 1: After modifying the file on the master branch and adding the local library, continue to modify the file helloworld.c on the master branch. At this time, it is not submitted to the staging area. I want to switch to the leaf01 branch and submit it to the leaf01 branch repository. This prompt appears:

The answer here is that if you modify a file on the master branch and the file has been changed but not submitted, you cannot switch branches;

Question 2: Modify the file on the master branch, then switch to the leaf01 branch and check the file content. It is found that it is the same as the content modified on the master branch;

Analysis:

  • The first branch modifies the file content without adding or committing, and then switching to the second branch will appear.

1. It prompts that there are file modifications that have not been submitted, and the branch cannot be switched;

2. You can switch branches, but the content of the second branch is overwritten by the modifications of the first branch;

  • Modify the content of the first branch, add it to the staging area, do not commit it to the local warehouse, and switch to the second branch, it will also appear.

1. Unable to switch branches;

2. The second branch is covered by the first branch;

  • After modifying the first branch, add and commit, switch to the second branch, and there will be no overwriting.

Solution:

1. Switch to which branch you want to submit the content to, and then submit the staging area and local library;

2. If the work on the current branch has not been completed and the local library cannot be submitted, and you want to go to another branch (if you directly switch to other branches, the contents of other branches may be overwritten!) :

Use the git stash command to hide the worksite;

git stash //隐藏当前分支的修改(在当前分支修改文件之后,不提交本地库也能切换其他分支)
git stash list //查看隐藏起来的分支工作现场
git stash apply //恢复,但是stash内容不删除,要用git stash drop来删除
git stash pop   //恢复同时stash内容删除;
---------
用法:git stash pop stash @{具体序号}

1-4-5. Branch push and pull operations

Order:

=========分支的push与pull操作
git branch -a      //查看本地和远程分支
git push <远程库名> <远程分支名>  //推送分支到远程(和推送代码一样命令)
git push <远程库名> :<远程分支名>     //删除远程分支(本地分支保留)(这个要谨慎操作,就差一个冒号)
git checkout -b <本地分支名> <远程库名/远程分支名> //拉取远程指定分支并在本地创建分支

----
git fetch //用来获取分支最新状态

View the remote branch:

To push a local branch to a remote branch , the instructions are the same as pushing the code, that is, on the local branch to be pushed to the remote branch, execute git push <remote warehouse name> <remote branch name>, and then push it;

 Pull the remote specified branch to create locally : (Application scenario: For example, there was a git project workspace on the computer. Originally there was only one master and one main branch, but now a new module is developed and placed on the new remote branch (such as the leader created ), this requires pulling the remote branch and creating the local original git workspace):

Create a new branch on github to pull local operation experiments 

git fetch shows that a new branch has been created remotely:

View all branches, pull remote branches and create new ones locally, view all branches

1-5. Idea integrates git

2、GitHub

2-1. Create a remote library

2-1-1. Register a github website account

Website: github official website

Register an account

2-1-2. Create a remote library

Click the + sign in the upper right corner

Select New repository

Enter the name and description of the remote library, select private and not public, and add the readme and gitignore files below. This is just for demonstration. Do not add them.

2-1-3. Add remote library and create alias

Order:

git remote -v                                      //查看和本地关联的仓库信息
git remote add <要创建的别名>  <原来远程库名的链接>   //给本地添加远程库链接,且远程库创建别名
git remote rename  <旧远程库名>  <新远程库名>        //重命名远程库
git remote rm <远程库名>                            //删除远程库与本地连接
---------
注:
1、原来的远程库名即为,如github网站上面创建的远程库[https://github.com/buggetout/git_demo.git]
这个链接即为远程名,可以用git remote add 来创建别名;


The git remote command is used to create, view and delete connections between local repositories and remote code repositories;

Operation of this machine:

As follows, a remote library has been created. Behind the HTTPS below [https://github.com/buggetout/git_demo.git ] is the address of the remote library.

Add the remote library and rename the remote library:

 

Rename the remote library name and delete the remote library operation

2-2. Code remote push push

2-2-1. Command:

git push <远程库名>  <分支名>  //推送到远程库

Operation of this machine:

After the local library is pushed to the remote library, the window on the left pops up.

Click Authorize:

After submitting the authorization, you can see the new project file on github.

2-3. Code pull pull

2-3-1. Command:

git pull <远程库名> <分支名>   // 拉取指定远程库指定分支的代码

Operation of this machine:

As shown in the figure below, when using the git pull command, the first error is reported because the pull fails due to the external network of github. Re-git pull succeeds.

2-4. Code clone clone

Copy the remote warehouse to the local for development. If you want to use the fixed directory as the working directory, and if the remote warehouse has been created by the leader, you can directly copy it to the local using the git clone command. The copied copy will be the entire version library. So there is no need to initialize with Git init; for example, if you want to git clone a remote library in the F:\_2_GitSpace\_1_github\git_demo_clone directory, you only need to right-click in the current directory to open git bash, and then git clone <remote library url>

2-4-1. Command:

git clone <远程库的url>   //克隆远程库到本地

2-4-2. What does the git clone operation do?

1. Pull the code

2. Initialize the local library

3. Create an alias

Operation of this machine:

Create a remote library on github to clone to local operation:

What is copied in this way is that the entire version library is copied;

2-4-3. What is the difference between git clone, git pull and git fetch:

[ The use and differences of git clone, git pull and git fetch_Gong Changbaishui's Blog-CSDN Blog ]

2-5. SSH password-free login

See details:

[The difference and use of HPPTS and SSH protocols in git_Gong Changbaishui's blog-CSDN blog]

2-6. Idea integrates github

3. Gitee code cloud

3-1. gitee creates a remote library

3-2. Idea integrates gitee

3-3. gitee connects to github to copy and migrate code

4、GitLab

4-1. Construction and deployment of gitlab server

4-2. Idea integrates gitlab

Guess you like

Origin blog.csdn.net/weixin_42640280/article/details/127039600