关于[git reset]

git reset

git reset < mode > < commit >

Set the current branch head (HEAD) to <commit>,optionally modifying index and working tree to match.

This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>. If <mode> is omitted,defaults to --mixed.

Step 1: Move HEAD
The first thing reset will do is move what HEAD points to.
This means if HEAD is set to the master branch, running git reset 9e5e6a4 will start by making master point to 9e5e6a4.
# HEAD依然指向master,但是改变master指向,master指向的不再是最新的commit

Step 2: Updating the Index (--mixed)
The next thing reset will do is to update the index with the contents of whatever snapshot HEAD now points to.

Step 3: Updating the Working Directory (--hard)
The third thing that reset will do is to make the working directory look like the index.

在这里插入图片描述

#Does not touch the index file or the working tree at all, but resets the head to <commit>
#相当于取消了上一次的commit,可以使用这条指令(当发现上一次的提交需要进行修改时)
#回到上一次git add完之后,但还没commit的时刻,即(index和工作目录中的内容一样,但和本地库中的内容不同)
git reset --soft#仅移动HEAD指向分支的指向(即只移动master)

在这里插入图片描述

# Resets the index but not the working tree, This is the default action.
git reset --mixed# 移动HEAD分支指向, 用HEAD分支指向的当前快照的内容来更新索引

在这里插入图片描述

# Resets the index and working tree.
# Any changes to tracked files in the working tree since <commit> are discarded.
git reset --hard# 移动HEAD指向, 用HEAD分支指向的当前快照的内容来更新索引, 更新工作目录

Reset With a Path

# 指定path,reset会跳过第一步,将剩余的操作限定到特定的文件或文件集.
If you specify a path,reset will skip step 1,and limit the remainder of its actions to a specific file or set of files.
# HEAD不能移动,但是index和工作区可以部分更新
HEAD is just a pointer, and you can’t point to part of one commit and part of another.
But the index and working directory can be partially updated, so reset proceeds with steps 2 and 3.

# 这三个一样
git reset file.txt
git reset HEAD file.txt
git reset --mixed HEAD file.txt

which will:
1.Move the branch HEAD points to (skipped).(跳过step1,不移动HEAD指向)
2.Make the index look like HEAD (stop here).(用HEAD指向的内容填充index)
it essentially just copies file.txt from HEAD to the index.

这实际上是取消暂存的效果
在HEAD之后,我们进行了文件修改,并且git add,还没commit
此时git reset该文件(用HEAD的内容填充index),就是取消上一步的git add

# 不从HEAD中提取,而是从eb43bf中提取
git reset eb43bf file.txt
Git version 2.23.0 introduced a new command: git restore.
From Git version 2.23.0 onwards, Git will use git restore instead of git reset for many undo operations.
# 等价于前面的 git reset <file>
git restore --staged <file>

git reset 三种模式
撤销Git已经push的操作
git删除push到远程服务器的commit
git reset 文档
gitReset

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值