Git系列之Git Reset

news/2025/2/26 21:15:14

目录

Git Reset

归属系列:


Git Reset

git中最常用的命令,但也是最危险,最容易被误用的命令。

git reset [<mode>] [<commit ID>], it means that

git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit ID>

This form “git reset” 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. The <mode> must be one of the following:

该格式的“git reset”重置当前的head到commit,可能更新索引(重置到commit树),工作树(工作目录)取决于模式mode。如果模式省去,则默认为mixed模式。模式必须是以下几种。

 

关于上图的名词解释:Working Copy 当前工作(树)目录下的文件,一般而言:有修改,既没有git add,也没有git commit的文件

  1. Index: staging area(暂存区)文件, 即git add 添加后的文件, 但没有git commit。文件commit之后,它就是当前HEAD的父节点。
  2. HEAD: 指向当前branch最新的commit,即该分支最近一次commit后的节点
  1. 当你首次checkout一个新的分支,HEAD默认指向该分支上最近一次commit。它的index和working copy是一样的(未曾有文件被改动)。
  2. 当你修改了一个文件,git会标记改动的文件,working copy不再和index和HEAD相同。
  3. 当你执行git add命令,会将上面修改的文件缓存在index中,此时working copy和index相同,而他们和HEAD不同。
  4. 当你执行git commit后,Git创建了一个新的commit,HEAD这时指向这个新的commit,此时,HEAD & index & working copy相同。

Git reset主要的参数有soft、 mixed、hard, 它们告诉Git,当执行reset时,要对index和working copy做什么。

  • --soft

不改变index和工作树的内容,只将HEAD重置到commit(就像所有模式都做的一样)。这使得你所有变更的文件变成“有改变需要被提交”。

For example, git reset --soft [commit ID] 回退到某个版本,只回退了commit的信息。如果还要提交,直接commit即可

  • --mixed(default)

If -N is specified, removed paths are marked as intent-to-add (see git-add [1]).

重置Head和index,但不影响工作树(当前工作目录下改动的文件被保留,但不会被标记用来commit),并且报告哪些没有被更新。这是默认行为。

指向那个你要reset到的commit上。而working copy文件不被改变。当然会显示工作目录下有修改,但没有缓存到index中。

Note: 用于重置暂存区的文件与某次历史提交(commit)保持一致,工作区文件内容保持不变

  • --hard

重置HEAD & index & working copy(同时改变到你要reset到的那个commit上)。Commit之后任何改变的文件都会被丢弃。这个参数很危险,你的本地修改可能都会丢失。

  • --merge

Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between <commit> and the index has unstaged changes, reset is aborted.

In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged index entries.

  • --keep

Resets index entries and updates files in the working tree that are different between <commit> and HEAD. If a file that is different between <commit> and HEAD has local changes, reset is aborted.

  • --[no-]recurse-submodules

When the working tree is updated, using --recurse-submodules will also recursively reset the working tree of all active submodules according to the commit recorded in the superproject, also setting the submodules' HEAD to be detached at that commit

注意:

  1. commitId 是每次commit的id,可以通过git log来进行获取
  2. 恢复git reset —hard 的误操作

如果通过 git reset --hard将版本回退到之前的commit。可以先通过 git reflog命令,找到要退回的commit Id,然后通过命令git reset —hard commit Id来恢复。

  1. git revert 和 git reset的区别
  2. git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit。
  3. 就回滚这一操作上,效果差不多。但是在日后继续merge以前的老版本时有区别。因为git revert是用一次逆向的commit “中和”之前的提交,因此日后合并老的branch时,导致这部分改变不会再次出现,但是git reset是之间把某些commit在某个branch上删除,因而和老的branch再次merge时,这些被回滚的commit应该还会被引入。
  4. git reset 是把HEAD向后移动了一下,而git revert是HEAD继续前进,只是新的commit的内容和要revert的内容正好相反,能够抵消要被revert的内容。
  5. 将本地某个分支强制更新到远程分支最新的commit
git checkout -b <branchname>

git reset --hard origin/<branchname>

归属系列:

LINK:<li><a href="文章链接">文章标题</a></li>


http://www.niftyadmin.cn/n/5869172.html

相关文章

Java+SpringBoot+Vue+数据可视化的铁路售票自动分配座位系统(程序+论文+讲解+安装+调试+售后)

感兴趣的可以先收藏起来&#xff0c;还有大家在毕设选题&#xff0c;项目以及论文编写等相关问题都可以给我留言咨询&#xff0c;我会一一回复&#xff0c;希望帮助更多的人。 系统介绍 在经济全球化与信息时代的滚滚浪潮之下&#xff0c;互联网技术以令人惊叹的速度迅猛发展…

【学习笔记】计算机网络(四)

第4章 网络层 文章目录 第4章 网络层4.1 网络层的几个重要概念4.1.1 网络层提供的两种服务虚电路服务&#xff08;Virtual Circuit Service&#xff09;数据报服务&#xff08;Datagram Service&#xff09; 4.1.2 网络层的两个层面 4.2 网际协议 IP - IPv44.2.1 虚拟互连网络4…

redis小记

redis小记 下载redis sudo apt-get install redis-server redis基本命令 ubuntu16下的redis没有protected-mode属性&#xff0c;就算sudo启动&#xff0c;也不能往/var/spool/cron/crontabs写计划任务&#xff0c;感觉很安全 #连接到redis redis-cli -h 127.0.0.1 -p 6379 …

游戏引擎学习第123天

仓库:https://gitee.com/mrxiao_com/2d_game_3 黑板&#xff1a;线程同步/通信 目标是从零开始编写一个完整的游戏。我们不使用引擎&#xff0c;也不依赖任何库&#xff0c;完全自己编写游戏所需的所有代码。我们做这个节目不仅是为了教育目的&#xff0c;同时也是因为编程本…

对Docker部署的MySQL中的数据进行备份恢复

文章目录 1. 启动MySQL容器 2. 使用 docker exec 运行 mysqldump 2.1 备份单个数据库 2.2 备份所有数据库 3. 压缩备份文件 3.1 压缩备份单个数据库 3.2 压缩备份所有数据库 4. 自动化备份 4.1 编辑 crontab 文件 4.2 添加备份任务 5. 恢复备份 5.1 恢复单个数据库…

Docker 搭建 Nginx 服务器

系列文章目录 Docker 搭建 Nginx 服务器 系列文章目录前言一、准备工作二、设置 Nginx 容器的目录结构三、启动一个临时的 Nginx 容器来复制配置文件四、复制 Nginx 配置文件到本地目录五、删除临时 Nginx 容器六、创建并运行 Nginx 容器&#xff0c;挂载本地目录七、修改 ngin…

PyTorch常用函数总结(持续更新)

本文主要记录自己在用 PyTorch复现经典模型 过程中遇到的一些函数及用法&#xff0c;以期对 常见PyTorch函数 更加熟练~ 官方Docs&#xff1a;PyTorch documentation — PyTorch 2.6 documentation 目录 数据层面 torch.sign(tensor) torch.tensor(np.eye(3)[y]) torch.on…