Home > Development Tools > git > Case plus analysis! Let you summarize common Git operation commands

Case plus analysis! Let you summarize common Git operation commands

WBOY
Release: 2022-03-23 19:39:59
forward
1848 people have browsed it

This article brings you relevant knowledge about Git. It mainly summarizes the commonly used operating commands of git through cases and analysis. Git is an open source distributed version control system. Used to handle any project, small or large, quickly and efficiently. I hope it will be helpful to everyone.

Case plus analysis! Let you summarize common Git operation commands

Recommended study: "Git Tutorial"

1. Git Concept

Git is an open source distributed version control system, used to handle any small or large project quickly and efficiently. It is currently the most popular version management tool.

2. The main difference between SVN and Git

SVN is a centralized version control system, and the version library is centralized on the central server , and when working, you use your own computer, so you first need to get the latest version from the central server, and then work. After finishing, you need to push the work you have done to the central server. The centralized version control system must be connected to the Internet to work. If it is on a local area network, it is okay, the bandwidth is large enough, and the speed is fast enough. If it is on the Internet, if the network speed is slow, it will be confusing.

Git is a distributed version control system, so it does not have a central server. Everyone's computer is a complete version library, so you don't need to be connected to the Internet when working. Because the versions are all on your own computer. Since everyone's computer has a complete version library, how can multiple people collaborate? For example, if you have modified file A on your computer, and someone else has also modified file A on your computer, you only need to push your modifications to each other, and you can see each other's modifications.

3. Install Git on Windows

Git was first developed on Linux. For a long time, Git could only run on Linux and Unix systems. . However, slowly someone ported it to Windows. Now, Git can run normally on the major platforms of Linux, Unix, Mac and Windows.

1. Download Git

To use Git, the first step is of course to install Git. Download it from https://git-for-windows.github.io (if the Internet speed is slow, please move to the domestic mirror), and then install it according to the default options.

2. Installation steps

After the download is complete, open it for installation (configure as shown below).
Case plus analysis! Let you summarize common Git operation commands

#3. Installation successful

Then you just need to wait quietly for the installation to complete, After completion, right-click on the desktop or a blank location in any folder. When the two menu bars shown in the figure below appear, the installation is successful.

Case plus analysis! Let you summarize common Git operation commands

4. Settings

After installation, a command box will pop up, we still need to do the last step In one step, set the identifier. Since git is a distributed management tool, you need to set the user name and email address as identification. Just enter the following code in the pop-up box.

git config --global user.name "Your Name"git config --global user.email "email@example.com"
Copy after login

Note: git config --global parameter. With this parameter, it means that all Git repositories on your machine will use this configuration. Of course, you You can also specify different user names and emails for a certain warehouse.

4. Analysis of common Git commands

1. Initialization

Before operating Git You have to create a Git repository first and create an empty folder in the location you need. Then enter the folder, right-click on the blank space, and click Git Bash Here to perform Git operations on the current folder.
Case plus analysis! Let you summarize common Git operation commands
Case plus analysis! Let you summarize common Git operation commands

  • 初始化命令
git init
Copy after login
  • 常用命令
cd:进入某个目录
mkdir:创建一个文件
pwd:显示当前的目录路径
鼠标选中就是复制,粘贴可以右键粘贴,也可以用使用快捷键:Shift+INS
Copy after login

2. 添加文件到版本库

2.1 添加到暂存区

  1. 在根目录新建了一个 a.txt 文件,内容为 aaa;
  2. 使用下面命令把 a.txt 文件添加到暂存区
  • 添加到暂存区命令
git add a.txt
Copy after login

Case plus analysis! Let you summarize common Git operation commands

  • 添加当前根目录下的所有文件到暂存区
git add .
Copy after login

2.2 提交到仓库

  1. 使用下面命令把暂存区的文件提交到版本库
git commit -m "双引号里面是注释——你的提交说明"
Copy after login

Case plus analysis! Let you summarize common Git operation commands

2.3 为什么 Git 添加文件需要 add、commit 两个步骤呢?

原因commit可以一次提交很多文件,所以你可以多次add不同的文件

例如

git add file1.txt					
#单个添加文件到暂存区git add file2.txt file3.txt			
#多个添加文件到暂存区git add .   						
#添加当前文件夹下所有文件到暂存区git commit -m "add 3 files."		
#提交所有暂存区的文件
Copy after login

3 查看文件状态(检查是否有未提交文件)

3.1 状态一

使用下面命令检查当前文件状态

git status
Copy after login

Case plus analysis! Let you summarize common Git operation commands
结果:没有需要提交的文件了;

3.2 状态二

创建一个新的文件 b.txt,内容为 bbb,再来检查文件状态
Case plus analysis! Let you summarize common Git operation commands
结果存在未跟踪文件没有添加到暂存区和提交到版本库;

3.3 状态三

添加 b.txt 到暂存区之后,再来检查文件状态
Case plus analysis! Let you summarize common Git operation commands
结果暂存区中有一个新的 b.txt 文件没有添加到版本库中;

3.4 状态四

提交 b.txt 到版本库之后,然后把 b.txt 内容从 bbb 修改为 bbba,再来检查文件状态
Case plus analysis! Let you summarize common Git operation commands
结果被改变的文件 b.txt 没有添加到暂存区且没有提交

4. 查看提交历史

4.1 先把前面修改的 b.txt 文件添加并提交

git add b.txtgit commit -m "提交修改的 b.txt 文件"
Copy after login

Case plus analysis! Let you summarize common Git operation commands

4.2 查看历史

  1. 到目前为止我们一共进行了3次操作,提交 a 和 b,以及修改 b,查看历史命令如下
git log
Copy after login

Case plus analysis! Let you summarize common Git operation commands
注意使用上面命令信息多的话会进入 log 模式,想要退出,在英文输入法的前提下按 q 就可以退出了

  1. 如上图所示,每次提交都有版本号,时间等信息,这样看起来比较乱,出现的信息太多,看的眼花,我们可以使用下面命令得到精简版的日志记录,让历史记录一行显示
git log --pretty=oneline
Copy after login

Case plus analysis! Let you summarize common Git operation commands

  1. 可以对想要查看的历史日志进行约束,比如想看最近提交的一条历史日志信息
git log -1
Copy after login

Case plus analysis! Let you summarize common Git operation commands

4.3 查看提交历史命令总结

git log						
#查看全部历史提交记录git log --pretty=oneline	#精简显示所有历史提交记录git reflog					
#可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)git log -p					
#查看全部提交历史并展示每次修改的内容git log -2					
#查看最近2次提交历史(注意:后面的数字是可以自定义的,也就是说,这种写法是 git log -n 的体现)git log -p -2				
#查看最近2次提交历史并展示修改的内容git log --stat				
#查看提交历史,并展示摘要内容(摘要会列出修改的文件以及每个文件中修改了多少行)
Copy after login

5. 版本回退与恢复

5.1 回退到上一个版本或上几个版本

  1. 首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
git reset --hard HEAD^
Copy after login

Case plus analysis! Let you summarize common Git operation commands

  1. 查看回退后的历史版本信息
    Case plus analysis! Let you summarize common Git operation commands
    结果从历史版本信息我们可以看出,我们已经从 “修改 b.txt” 回退到了 “提交 b.txt”,回退之后的当前版本为 “提交 b.txt”

5.2 回退到指定版本

  1. 我们直接复制想要回退位置的版本号,使用下面代码就可以回退到复制的版本号的位置了
git reset --hard 
Copy after login
  1. 例如:我复制 “提交 a.txt 文件” 的版本号
git reset --hard 6ec9373d22d1a869b67681a39dc04df34292133b
Copy after login

Case plus analysis! Let you summarize common Git operation commands
结果从查看的历史版本结果可以看出我们回退到了 “提交 a.txt” 版本

5.3 恢复回退的版本

  1. 有时候我们回退了版本后反悔了,想要返回到回退版本之前,这时我们就需要找到之前的 commit id,但是从上面的查看历史版本命令可以看出,我们找不到之前的 commit id 了,于是我们可以使用下面命令
git reflog
Copy after login

Case plus analysis! Let you summarize common Git operation commands

结果reflog 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)

  1. 假设我们想回退到 “提交修改的 b.txt 文件” 的位置上,也就是历史版本信息的倒数第三行,我们只需要复制 HEAD 前面的 7a42e7b,然后使用下面命令就可以了
git reset --hard 7a42e7b
Copy after login

Case plus analysis! Let you summarize common Git operation commands

结果历史版本信息可以看出我们已经回退到了我们想要的版本

6. 查看不同版本的差异

  1. 当我们们修改完代码之后没有添加到暂存区提交,第二天我们忘记了昨天改了什么代码,这是我们就可以按下面步骤来看不同版本之间的差异;

例如:我们把 b.txt 文件内容修改为为 bbbaaa,然后用下面代码查看,可以看出我们修改了什么

git diff		# 查看不同版本之间的文件差异
Copy after login

Case plus analysis! Let you summarize common Git operation commands

7. 修改与撤销

7.1 修改文件

推荐使用:第一次修改 -> git add -> 第二次修改 -> git add -> git commit

注意建议每次 commit 之前先检查是否有文件没有被 add

7.2 撤销修改

git checkout -- filename
Copy after login

git checkout -- filename可以丢弃工作区的修改:– 后面是一个空格

命令 git checkout -- readme.txt 意思就是,把 readme.txt 文件在工作区的修改全部撤销,这里有两种情况:
一:readme.txt 自修改后还没有被放到暂存区(git add),现在,撤销修改就回到和版本库一模一样的状态;
二:readme.txt 已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

总之,就是让这个文件回到最近一次 git commitgit add 时的状态。

注意git checkout -- file 命令中的 -- 很重要,没有 -- ,就变成了**“切换到另一个分支”**的命令,我们在后面的分支管理中会再次遇到 git checkout 命令

8. 删除文件/缓存

8.1 使用 rm 命令删除文件

通常直接在文件管理器中把没用的文件删了,或者用rm命令删除,例如:删除 b.txt

git rm b.txt
Copy after login

删除步骤

  • rm 删除文件
  • rm 删除后会自动添加到暂存区,省略了手动 add 命令
  • 最后进行 commit 提交,文件被删除
    Case plus analysis! Let you summarize common Git operation commands
    注意删除步骤可以通过 reset 撤销操作撤回

8.2 删除缓存区

git rm b.txt --cached
Copy after login

b.txt 处于未跟踪状态,也就是从暂存区删除。

特别说明处于未跟踪状态只是没有存在于暂存区,历史提交记录中的记录依然存在。

8.3 清空缓存

所谓的暂存区仅仅是.git目录下的一个index文件罢了,这也是为了什么被称为index(索引),当删除暂存区内容的时候,其实就是删除index文件中的内容,.git/objects目录中的内容不会被删除。

rm .git/index
Copy after login

9. 分支管理

9.1 分支操作命令

git branch						
#查看分支 git branch <name>				
#创建分支git checkout <name>				
#切换分支git checkout -b <name>			
#创建 + 切换分支git merge <name>				
#将某分支合并到当前分支git branch -d <name>			
#删除分支</name></name></name></name></name>
Copy after login

Case plus analysis! Let you summarize common Git operation commands

9.2 开发流程图

Case plus analysis! Let you summarize common Git operation commands

五、Git 命令整合

1. 设置全局用户名和邮箱

命令 作用
git config --global user.name “Your Name” 设置用户名
git config --global user.email “email@example.com” 设置邮箱

2. 初始化命令

命令 作用
git init 初始化 git,创建 .git 文件

3. 常用命令

命令 作用
cd 进入某个目录
mkdir 创建一个文件
pwd 显示当前的目录路径
鼠标选中就是复制 复制
直接鼠标右键粘贴 / 快捷键:Shift+INS 粘贴

4. 添加到暂存区

命令 作用
git add a.txt 添加 a.txt 到暂存区
git add . 添加当前根目录下的所有文件到暂存区
git commit -m “双引号里面是注释——你的提交说明” 把暂存区的文件提交到版本库(一次全部提交)

5. 查看文件状态

命令 作用
git status 查看文件状态(检查是否有未提交文件)

6. View submission history

Command Function
git log View all historical commit records
git log --pretty=oneline Concise display of all historical commit records
git reflog You can view all operation records of all branches (including Deleted commit records and reset operations)
git log -p View all submission history and display the contents of each modification
git log -2 View the last 2 commit history (note: the following numbers can be customized, that is, this way of writing is git log The embodiment of -n)
git log -p -2 View the last 2 submission history and display the modified content
git log --stat View the commit history and display the summary content (the summary will list the modified files and how many lines were modified in each file)

7. Version rollback and recovery

##CommandFunctionGo back to the previous versionReturn to the previous N versionsRoll back to the specified version to restore the rolled back version
git reset --hard HEAD^
git reset --hard HEAD~N (not -, it is a wavy line)
git reset --hard
git reflog and git reset -- hard Merge and use

8. View the differences Version differences

CommandFunctionView file differences between different versions
git diff

9. Undo changes

CommandFunction##git checkout – b.txt readme.txt
1: has not been placed in the temporary storage area (git add) since the modification. Now, undo the modification and return to the The repository is in exactly the same state; 2: readme.txt
has been added to the staging area and modified. Now, undoing the modification will return to the state after being added to the staging area.
10. Delete the


commandgit rm b.txtrm will be automatically added to the temporary cache after deletion area, the manual add command is omitted##git rm b.txt --cachedrm .git/index11. Branch management
effect
rm Delete the fileFinally commit, the file is deleted

Delete a single file in the cache area, b.txt is in an untracked state, that is, delete
from the temporary storage area Clear all cache


CommandFunctiongit branchgit branch git checkout git checkout -b git merge git branch -d Recommended learning: "Git Learning Tutorial
View branch
Create branch
Switch branch
Create and switch to a new branch
Merge a branch to the current branch
Delete branch
"

The above is the detailed content of Case plus analysis! Let you summarize common Git operation commands. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
git
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template