git reset hard,mixed,soft

2023-11-21 11:36
文章标签 reset git hard soft mixed

本文主要是介绍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.

这篇关于git reset hard,mixed,soft的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/402115

相关文章

(超详细)YOLOV7改进-Soft-NMS(支持多种IoU变种选择)

1.在until/general.py文件最后加上下面代码 2.在general.py里面找到这代码,修改这两个地方 3.之后直接运行即可

Git的安装以及使用

一.简单介绍 1.1版本控制 版本控制是指对软件开发过程中各种程序代码,配置文件及说明文档等文件变更管理,是软件配置管理的核心思想之一。 版本控制最重要的内容是追踪文件的变更,它将什么时候,什么人更改了文件的什么内容等信息忠实的记录下来。除此之外,版本控制的另一重要的功能是并行开发。软件开发往往是多人协同作业,版本控制可以有效地解决版本的同步以及不同开发者之间的开发通信问题,提高开发效率。

Git代码管理的常用操作

在VS022中,Git的管理要先建立本地或远程仓库,然后commit到本地,最后push到远程代码库。 或者不建立本地的情况,直接拉取已有的远程代码。 Git是一个分布式版本控制系统,用于跟踪和管理文件的变化。它可以记录文件的修改历史,并且可以轻松地回滚到任何历史版本。 Git的基本概念包括: 仓库(Repository):Git使用仓库来存储文件的版本历史。一个仓库可以包含多个文件

nginx 8051#0: *4 recv() failed (104: Connection reset by peer) while reading response header from u

环境    php7   nginx1.8.0    nginx   报错  500  GATWAY网关错误 2017/08/28 10:45:42 [error] 7975#0: *333 recv() failed (104: Connection reset by peer) while reading response header from upstream, clien

通过sockst下载git

设置 Git 使用 SOCKS5 代理 git config --global http.proxy “socks5://localhost:1080” git config --global https.proxy “socks5://localhost:1080” 克隆仓库 git clone https://github.com/odoo/odoo.git 取消代理设置(可选) g

git clone [github]下载速度慢问题解决

将下面两个ip加入HOST 192.30.253.113 github.com 151.101.185.194 github.global.ssl.fastly.net 如果不起作用,登录 http://github.com.ipaddress.com/ (需要翻墙)输入 github.global.ssl.fastly.net 和 github.com查询对应的ip,修改host文件中的i

git fetch.git merge,git pull

git fetch origin branch1:branch2 首先执行上面的fetch操作 使用远程branch1分支在本地创建branch2(但不会切换到该分支),  如果本地不存在branch2分支, 则会自动创建一个新的branch2分支,  如果本地存在branch2分支, 并且是`fast forward', 则自动合并两个分支, 否则, 会阻止以上操作. Git pus

Eclipse使用git最基本流程

Eclipse使用git最基本流程,eclipsegit流程 git有诸多好处,网上都说的很清楚了,在这里我不再赘述。对于我来说,私下里想做一些项目,而又不能很好的保存自己的代码和进行版本控制,这时候,就用到了git。下面,就以我个人为例讲讲git从0开始如何安装使用。 Step1 准备工作 msysgit,下载地址为http://msysgit.github.io/

【已解决】在IDEA中使用Git拉取代码时提示:Can‘t update / master has no tracked branch

文章目录 问题描述原因分析解决方案 问题描述 在IDEA中使用Git拉取代码,尝试更新本地项目代码,提示 " Can't update / master has no tracked branch ",如下图所示: 原因分析 出现上述问题意味着本地名为master的分支(或者在很多新项目中为了更包容性已改为main作为默认分支名)并没有设置为跟踪任何远程分支。

Git 中 pull 操作和 rebase 操作的不同

由于在开发过程中,pull 操作和 rebase 操作都是用来合并分支的,所以我就常常分不清这两个操作具体有什么区别,所以才有了这篇博客来做个简单区分,具体细致差别还请移步到官方文档:Git - Reference (git-scm.com) 1)pull 操作明确来说,实际是分为了两步操作:fetch + merge fetch:进行 pull 操作的时候,git 首先会将远程仓库中的所有远