git reset hard,mixed,soft

首先,我们得了解git reset命令的形式之一:

git reset [<mode>] [<commit>]

此命令的作用是恢复HEAD分支到<commit>位置,并根据<mode>决定是否恢复index file和working tree。恢复是指将staging area和working tree的状态还原到commit的状态(如果不指定commit,则默认为last commit)。如果没有pick哪个mode,即omitted mode,则mode默认为--mixed。<commit>可以为commit的hash id或引用值。

此外,mode还有两个值:--soft和--hard。

--soft不会改动(touch) index file和working tree,但是会撤销head到commit之间的all history,这是三个模式所共有的作用。

bash 复制代码
$ git status

On branch main

Changes not staged for commit:

 (use "git add <file>..." to update what will be committed)

 (use "git restore <file>..." to discard changes in working directory)

​    modified:  README.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git add README.md

$ git status

On branch main

Changes to be committed:

 (use "git restore --staged <file>..." to unstage)

​    modified:  README.md

$ git log --graph --oneline

\* 0a071ac (HEAD -> main) A

\* 0c888f4 Initial Commit

$ git reset --soft 0c888f4

$ git status

On branch main

Changes to be committed:

 (use "git restore --staged <file>..." to unstage)

​    modified:  README.md

$ git log --graph --oneline

\* 0c888f4 (HEAD -> main) Initial Commit

可以观察到commit A的committed changes都reset为staged changes to be committed。原本已经stage等待commit的changes不会被reset。HEAD指向<commit>,HEAD到<commit>的提交都会被undo。

--mixed在--soft的基础上,进一步reset,它会恢复index到<commit>时的状态。已经在index中的changes会reset回working tree,note that modify working tree≠reset working tree。

bash 复制代码
$ git reset 0c888f4

Unstaged changes after reset:

M    README.md

git reset会报告影响到的changes。

--hard会在--mixed的基础上恢复working tree的状态。被跟踪的changes会被丢弃。

Any untracked files or directories in the way of writing any tracked files are simply deleted.

Git文档中的这句话我不是太理解,通过询问ChatGPT,翻译为:任何阻碍跟踪文件的写入的未跟踪文件或目录将被删除。

下面是一个Stack Overflow对该疑问的解释代码例子:

bash 复制代码
There is a case in which git reset --hard has effects on untracked files.

$ touch a.txt

$ git add .

$ git commit -m'A'

$ echo hello >> a.txt

$ git add .

$ git commit -m'B'

$ git rm a.txt

$ echo world > a.txt

$ git status -s

The status output is

D  a.txt

?? a.txt

The a.txt in the index is removed and the one in the work tree is untracked.

$ cat a.txt

world

Reset in the mode of --hard,

$ git reset --hard

$ cat a.txt

hello

$ git status

On branch master

nothing to commit, working tree clean

The untracked a.txt in the work tree is deleted. But we could also say it's been overwritten with the tracked a.txt in HEAD.

相关推荐
OsDepK5 小时前
项目快速Git至仓库(完整版)
大数据·git·elasticsearch·搜索引擎
妙码生花7 小时前
使用git更新ai-go-admin框架
前端·人工智能·git·golang·typescript·php
朝阳5819 小时前
如何用 AI 自动生成规范的 Git 提交信息:从暂存区 diff 到 Conventional Commit
人工智能·git
hudawei9969 小时前
有未保存的修改,不能切换分支
git·git stash·stash
康一夏9 小时前
Git Commit 规范实践:Angular Commit 规范 + Commitlint 自动化验证
git·自动化·commit msg·commit规范
LayZhangStrive13 小时前
Git - 使用IDEA和git上传本地项目到Gitee远程仓库
git·gitee·idea
云泽80814 小时前
Git 版本控制系统(上):架构原理与操作入门
linux·git·架构
绝世唐门三哥14 小时前
Git - git stash 语法演进与实战指南
前端·git
梨想橙汁15 小时前
Git 分支详解:创建切换、合并分支、冲突解决实战
前端·git
那年窗外下的雪.1 天前
AIDC 学习日志|第 21 天|EVPN 多归属故障演练与中断窗口定位
网络·git·学习