当前位置: 首页 > news >正文

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,这是三个模式所共有的作用。

$ git statusOn branch mainChanges 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.mdno changes added to commit (use "git add" and/or "git commit -a")$ git add README.md$ git statusOn branch mainChanges 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 statusOn branch mainChanges 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。

$ git reset 0c888f4Unstaged 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对该疑问的解释代码例子:

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 -sThe status output isD  a.txt?? a.txtThe a.txt in the index is removed and the one in the work tree is untracked.$ cat a.txtworldReset in the mode of --hard,$ git reset --hard$ cat a.txthello$ git statusOn branch masternothing 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.

http://www.lryc.cn/news/240222.html

相关文章:

  • Cookie与Session知识
  • Vue批量全局处理undefined和null转为““ 空字符串
  • 【2023年APMCM亚太杯C题】完整数据与解题思路
  • 嵌入式单片机方向和Linux驱动开发方向哪个发展前景好?
  • 如何搭建Zblog网站并通过内网穿透将个人博客发布到公网
  • 2:kotlin集合(Collections)
  • 小诺2.0开源版工程启动
  • idea手动导入maven包
  • 2、单片机及开发板介绍
  • Leetcode 第 372 场周赛题解
  • mysql查询统计最近12个月的数据
  • 14.Python 模块
  • 三十分钟学会Linux的基本操作
  • 1688商品详情数据接口(1688.item_get)
  • SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐:SpringCloud Gateway核心技术
  • 设计师不能忽视的几个宝藏图标设计工具
  • 设计模式-行为型模式-模板方法模式
  • 露营管理系统预约小程序效果如何
  • 【产品安全平台】上海道宁与Cybellum将整个产品安全工作流程整合到一个专用平台中,保持构建的互联产品的网络安全和网络合规性
  • css 实现鼠标上移添加下划线
  • C语言--给定一个数组,把第一项的值减去第二项的值,第二项的值减去第三项的值,第三项的值减去第四项的值,依次类推。放到一个新的数组中,并打印新的数组
  • Vue+Swiper实现轮播图效果
  • 竞赛选题 行人重识别(person reid) - 机器视觉 深度学习 opencv python
  • 解决vue中引入天地图显示不全问题,设置setTimeout即可解决!
  • 【OpenCV实现图像:使用OpenCV进行物体轮廓排序】
  • 【8】Spring Boot 3 集成组件:安全组件 spring security【官网概念篇】
  • UDP中connect的作用
  • Go使用开源库go-excelize操作Excel文件
  • 软件测试个人求职简历该怎么写,模板在这里
  • opencv-Hough 圆环变换