git reset < mode > < commit >
Set the current branch head (
HEAD
) to,optionally modifying index and working tree to match.
This form resets the current branch head to
and possibly updates the index (resetting it to the tree of
) and the working tree depending on
. If
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,可以使用这条指令(当发现上一次的提交需要进行修改时)
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 are discarded.
git reset --hard# 移动HEAD指向, 用HEAD分支指向的当前快照的内容来更新索引, 更新工作目录
# 指定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
git restore --staged <file>
git reset 三种模式
撤销Git已经push的操作
git删除push到远程服务器的commit
git reset 文档
gitReset