Home > Development Tools > git > body text

Detailed explanation of git-reset for Git command learning

青灯夜游
Release: 2023-03-23 19:33:18
forward
2883 people have browsed it

Detailed explanation of git-reset for Git command learning

The function of git-reset is to reset the HEAD pointer of the current branch and point the HEAD pointer to a specific state.

Usage Overview

git reset [<tree-ish>] [--] <pathspec>
git reset [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>]
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [<commit>]
Copy after login

The function of the reset command in the first three lines is to use the specified content as a reference basis, and then copy the content to the target in the cache area.

It is something like a tree. There are many trees in git, such as commit tree or tag tree.

The function of the reset command in the last line is to point the HEAD pointer of the current branch to . At the same time, you can use a variety of options to control whether to modify the cache area or work area.

In all the above command forms, or is used as the HEAD pointer by default.

git reset [] [--]

##This method has the following rules:

  • will reset the path resources matching in the cache to the state in .

    Don’t know what is?

  • #Changes submitted to the cache area will be restored to the work area.

For example?

Suppose there is a coffee.txt file in the project. The content of the file is as follows:

卡布奇诺纳瑞冰-19.9¥
标准美式-14.9¥
香草拿铁-19.9¥
生椰爱摩卡-19.9¥
...
Copy after login
Copy after login

We use the git tag command to copy the current The version is marked as v1.0.0 (use tag tag here).

Then modify the coffee.txt file. The modification content is as follows:

卡布奇诺纳瑞冰-19.9¥ --> 卡布奇诺纳瑞冰-14.9¥
标准美式-14.9¥ --> 标准美式-9.9¥
香草拿铁-19.9¥ --> 香草拿铁-14.9¥
生椰爱摩卡-19.9¥ --> 生椰爱摩卡-14.9¥
...
Copy after login

After changing the coffee.txt file, use git tag to mark it with v1.0.1.

If you want to restore the coffee.txt file to the v1.0.0 version at this time, you can use the git reset command and specify it as v1.0.0. The operation is as follows:

git reset v1.0.0 coffee.txt
Copy after login

Use VSCode Check the file changes in the cache area:

Detailed explanation of git-reset for Git command learning

You can see the comparison of code changes on the right side of the picture. At this time, the coffee.txt file in the cache area has been successfully reset. Become the status of the file in v1.0.0 version (Rule 1). And at this time, the changes that v1.0.1 has submitted to the cache have also been restored to the working tree (Rule 2).

Detailed explanation of git-reset for Git command learning

It can be seen that the git reset command and the git add command have opposite effects. One is to add the specified resource to the cache area, and the other is to remove it from the cache area. And this command has the same effect as git restore [--source=] --staged . If you are interested in restore, you can read

this article.

As mentioned above, after using the reset command, the file content in the cache area is v1.0.0. You can use the git restore command to restore the contents of the cache area to the workspace and then modify it:

git restore coffee.txt --staged
Copy after login

You can also choose to restore the contents of the workspace as needed:

git restore coffee.txt --worktree
Copy after login

git reset [--pathspec-from-file= [--pathspec -file-nul]] []

In the above example, the same path (coffee .txt). Because the file path is relatively simple, it is easier to enter every operation. However, in some cases, you may need to reset a more complex path. For example, if the project directory is deep, you may need to enter a long list of paths, which will be very troublesome every time you perform the operation. So git provides a --pathspec-from-file option, which allows us to directly specify a file, which contains the path that may need to be reused.

Here has more detailed usage.

Each line of this file is a . If there are multiple , use newline characters as separation. Of course, you can also use --pathspec-file-nul to use NUL as the delimiter for each .

##git reset (--patch | -p) [] [--] []

交互式的选择与缓存区之间的差异而产生的hunks。这些被选择的hunks将会撤销缓存区中的产生的修改。这里有更详细的--patch选项用法。

git reset [] []

该命令会把当前分支的HEAD指针指向某个,然后由决定是否同时更新缓存区或工作区的内容。默认值是--mixed,且必须是以下几种:

--soft

工作区和缓存区中的文件变动都将被保留,然后将HEAD指针指向

还是以咖啡菜单为?,假如第一次commit到仓库中的文件内容如下:

卡布奇诺纳瑞冰-19.9¥
标准美式-14.9¥
香草拿铁-19.9¥
生椰爱摩卡-19.9¥
...
Copy after login
Copy after login

然后做第二次commit操作,删除标准美式,增加生椰拿铁:

卡布奇诺纳瑞冰-19.9¥
-标准美式-14.9¥
+生椰拿铁-19.9¥
香草拿铁-19.9¥
生椰爱摩卡-19.9¥
...
Copy after login

在commit后,修改卡布奇诺纳瑞冰的价格,添加到缓存区。再修改香草拿铁的价格,保留在工作区:

-卡布奇诺纳瑞冰-19.9¥
+卡布奇诺纳瑞冰-14.9¥ // 添加到缓存区中
生椰拿铁-19.9¥
-香草拿铁-19.9¥
+香草拿铁-14.9¥ // 保留在工作区
生椰爱摩卡-19.9¥
...
Copy after login

此时我想保留工作区和缓存区做的改动,并且将HEAD指针指向第一次commit。这时可以使用--soft选项实现:

git reset HEAD^ --soft // 这里使用HEAD^表示上一个commit,同样也可以使用hash id
Copy after login

使用git log查看当前HEAD指针确实已经指向第一个commit,第二个commit被抛弃了:

Detailed explanation of git-reset for Git command learning

同时所有的工作区和缓存区的改动都被保留了:

Detailed explanation of git-reset for Git command learning

当前只有两次的commit,是为了方便演示--soft选项的作用。但是在实际开发中,我们可能会commit多次,尤其是在测试环境改几个BUG就要提交到发布平台,这样会导致很多无意义的commit。这时候就可以使用--soft选项,重置HEAD到指定的只保留一到两个重要的commit。

--mixed

重置缓存区,但是会保留工作区的内容,这是的默认值。

相信在理解了--soft作用后,理解--mixed不难,上面例子中如果是使用--mixed那么最终结果如下:

Detailed explanation of git-reset for Git command learning

该选项会重置缓存区,但是保留工作区的改动,并将当前指针指向

--hard

重置缓存区和工作区中的所有的变动,并且将指针指向

--hard更加的简单粗暴,我们将--soft例子改为--hard来查看结果:

Detailed explanation of git-reset for Git command learning

如图所示,工作区和缓存区的内容都被重置了。不止是如此,就连Untracked文件同样也会被删除。

--merge

该选项的作用,看字面意思就知道大概就是把当前分支和指定的进行合并,规则如下:

  • 重置缓存区,任何已经添加到缓存区的改动都将被抛弃

  • 如果和HEAD之间有文件存在不同(这个不同指的是文件被删除或者新增),那么将会把该文件重置成中的状态(新增或删除)。

  • 如果和HEAD之间有文件存在不同(这个不同是指文件内容的不同),且此时工作区也存在未提交的改动,那么本次的reset将会被终止。

  • 如果一个文件在和HEAD中完全相同,但是它的工作区存与缓存区存在着不同(也就是改动未提交到缓存区),那么该文件在工作区的改动在重置之后就会被保留。

我们举个例子来验证一下以上列出的规则,假设此时的咖啡店项目有如下的几个commit。

第一个commit和文件内容如下:

Detailed explanation of git-reset for Git command learning

第一个commit中只有一个coffee.txt菜单文件,此时如果咖啡店引进了新品种开始卖果汁,那么就需要新增果汁菜单文件fruits.txt,于是就有了第二个commit:

Detailed explanation of git-reset for Git command learning

此时我们做一些改动来验证1,2,4这几点的规则,改动后的文件如下:

Detailed explanation of git-reset for Git command learning

首先修改coffee.txt文件,新增一款生椰拿铁咖啡,保留在工作区中。然后增加咖啡豆菜单文件beans.txt,将其添加到缓存区中。

假设因需求变动,咖啡豆菜单文件在缓存区中需要清除,果汁菜单文件需要删除,只有咖啡菜单中新增的生椰拿铁的改动需要保留。那么就可以使用git reset --merge将HEAD和commit-1进行合并,操作如下:

git reset --merge HEAD^
Copy after login

结果如下:

Detailed explanation of git-reset for Git command learning

执行命令reset命令产生了如下效果:

  • 将当前HEAD指针指向了commit-1
  • 将缓存区中的beans.txt文件抛弃(规则1)
  • HEAD(commit-2)和commit-1中的fruits.txt文件存在不同(commit-2中新增fruits.txt),所以会将fruits.txt删除(规则2)
  • coffee.txt文件新增生椰拿铁的改动被保留在工作区中(规则4)

再来验证一下第3点规则,假设咖啡店项目此时第一个commit如下:

Detailed explanation of git-reset for Git command learning

接下去同样新增水果茶菜单,然后再修改coffee.txt文件,第二个commit如下:

Detailed explanation of git-reset for Git command learning

然后在HEAD中再修改coffee.txt文件,删除掉标准美式品种:

Detailed explanation of git-reset for Git command learning

此时,如果我们再使用git reset --merge HEAD^就无法再进行重置,该操作会被git终止(规则3)。并且控制台会进行报错提示:

Detailed explanation of git-reset for Git command learning

--keep

该选参的作用和--merge相似,唯一的区别就是缓存区中被重置的会被保留在工作区中。

构造如下第一个commit:

Detailed explanation of git-reset for Git command learning

改造第二个commit:

Detailed explanation of git-reset for Git command learning

在HEAD中进行修改

Detailed explanation of git-reset for Git command learning

使用git reset --keep命令:

Detailed explanation of git-reset for Git command learning

从结果上来看,只有fruits.txt文件被删除了,beans.txt文件被重置回了工作区中。coffee.txt文件的改动也被保留了。

--[no-]recurse-submodules

使用该选项可以控制是否递归的重置submdoule。如果想要更详细了解,查看这篇文章

总结

git-reset命令的作用就是重置缓存区和工作区,同时它也提供多个选项来做更具体的控制,使得该命令更加灵活多变。git-reset命令在我们的工作中经常使用,因此熟练掌握该命令是非常重要的。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of Detailed explanation of git-reset for Git command learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!