git中reset、restore、checkout、revert、clean的用法和区别

2023-10-27

1. git reset

NAME

git-reset - Reset current HEAD to the specified state

SYNOPSIS

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

DESCRIPTION

set the current branch head (HEAD) to <commit>, optionally modifying index and working tree to match. 

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

This form 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:

--soft

Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

--mixed

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

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

--hard

Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.

--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.

See "Reset, restore and revert" in git(1) for the differences between the three commands.

总结:

reset 可以用来恢复历史版本commit

加上--hard参数,将暂存区与工作区的内容都“重置”至commit状态。

reset的影响范围包括当前git版本目录下的所有文件和目录,不能单独恢复单个文件。

注意:

对于非可跟踪的文件和目录(即未git add 进来的文件或文件夹),保持最新状态,不作任何改变。

例如:

手动新增了一个文件newfile.txt,reset之后,newfile.txt不会被删除,因为newfile.txt未被git add,它是不可跟踪的。


2. git restore

NAME

git-restore - Restore working tree files

SYNOPSIS

git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>…​
git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>…​]

DESCRIPTION

Restore specified paths in the working tree with some contents from a restore source. If a path is tracked but does not exist in the restore source, it will be removed to match the source.

The command can also be used to restore the content in the index with --staged, or restore both the working tree and the index with --staged --worktree.

By default, if --staged is given, the contents are restored from HEAD, otherwise from the index. Use --source to restore from a different commit.

总结:

checkout的功能很多,从 Git 2.23 版本开始引入了两个新的命令: git switch 用来切换分支,git restore用来还原工作区的文件。

restore可以分别对暂存区或工作区进行操作。

使用--staged参数,可以单独只恢复暂存区,也就是丢弃add到暂存区的状态,是将内容从commit恢复到暂存区,此时不影响工作区的内容,commit ----> 暂存区,如需进一步恢复工作区的内容,可以进一步执行git restore --worktree .;

使用--worktree,可以单独只恢复工作区,是将内容从暂存区恢复到工作区,而不是从commit恢复内容到工作区,暂存区 ----> 工作区!

也可以同时使用--staged --worktree,同时恢复暂存区和工作区,效果等同于git reset --hard.

也可以单独恢复git版本目录下的单独某一个文件

注意:

对于非可跟踪的文件和目录(即未git add 进来的文件或文件夹),保持最新状态,不作任何改变。

例如:

手动新增了一个文件newfile.txt,reset之后,newfile.txt不会被删除,因为newfile.txt未被git add,它是不可跟踪的。


3. git checkout

NAME

git-checkout - Switch branches or restore working tree files

SYNOPSIS

git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>…​
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>…​]

DESCRIPTION

Updates files in the working tree to match the version in the index or the specified tree. If no pathspec was given, git checkout will also update HEAD to set the specified branch as the current branch.

OPTIONS

-f

--force

When switching branches, proceed even if the index or the working tree differs from HEAD, and even if there are untracked files in the way. This is used to throw away local changes and any untracked files or directories that are in the way.

When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.

总结:

1. checkout 可以用来切换分支branch,等同于git branch.

2. checkout也可以用来恢复暂存区和工作区内容,等同于git reset

3. 切换版本:git checkout -f <COMMIT> 等同于git reset --hard <COMMIT>

-f参数 强制切换

注意:

身兼数职的 git checkout 命令现在可以轻松一些了,从 Git 2.23 版本开始引入了两个新的命令: git switch 用来切换分支,git restore用来还原工作区的文件。

对于非可跟踪的文件和目录(即未git add 进来的文件或文件夹),保持最新状态,不作任何改变。

例如:

手动新增了一个文件newfile.txt,reset之后,newfile.txt不会被删除,因为newfile.txt未被git add,它是不可跟踪的。


4. git clean

NAME

git-clean - Remove untracked files from the working tree

SYNOPSIS

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

DESCRIPTION

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.

If any optional <path>... arguments are given, only those paths are affected.

OPTIONS

-d

Normally, when no <path> is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well. If any paths are specified, -d is irrelevant; all untracked files matching the specified paths (with exceptions for nested git directories mentioned under --force) will be removed.

-f

--force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.

-i

--interactive

Show what would be done and clean files interactively. See “Interactive mode” for details.

-n

--dry-run

Don’t actually remove anything, just show what would be done.

-q

--quiet

Be quiet, only report errors, but not the files that are successfully removed.

-e <pattern>

--exclude=<pattern>

Use the given exclude pattern in addition to the standard ignore rules (see gitignore(5)).

-x

Don’t use the standard ignore rules (see gitignore(5)), but still use the ignore rules given with -e options from the command line. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git restore or git reset) to create a pristine working directory to test a clean build.

-X

Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

总结:

清除git版本库下的所有非可跟踪的文件和目录,可以包括新增的文件和目录。

-f 强制删除,清除时只包括当前目录下的文件,不包括新增的目录或次级目录下的任意文件,可以使用-f -d 参数,将所有新增的文件和目录全部删除。

-d 清除时包括目录一起删除。


5. git revert

NAME

git-revert - Revert some existing commits

SYNOPSIS

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…​
git revert (--continue | --skip | --abort | --quit)

DESCRIPTION

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).

Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset(1), particularly the --hard option. If you want to extract specific files as they were in another commit, you should see git-restore(1), specifically the --source option. Take care with these alternatives as both will discard uncommitted changes in your working directory.

See "Reset, restore and revert" in git(1) for the differences between the three commands.

OPTIONS

<commit>…​

Commits to revert. For a more complete list of ways to spell commit names, see gitrevisions(7). Sets of commits can also be given but no traversal is done by default, see git-rev-list(1) and its --no-walk option.

-e

--edit

With this option, git revert will let you edit the commit message prior to committing the revert. This is the default if you run the command from a terminal.

-m parent-number

--mainline parent-number

Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.

Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.

See the revert-a-faulty-merge How-To for more details.

--no-edit

With this option, git revert will not start the commit message editor.

--cleanup=<mode>

This option determines how the commit message will be cleaned up before being passed on to the commit machinery. See git-commit(1) for more details. In particular, if the <mode> is given a value of scissors, scissors will be appended to MERGE_MSG before being passed on in the case of a conflict.

-n

--no-commit

Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.

This is useful when reverting more than one commits' effect to your index in a row.

总结:

改变之前的commit中的一些内容,并自动生成一个新的commit。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

git中reset、restore、checkout、revert、clean的用法和区别 的相关文章

  • 使用 Git 部署时压缩 JS/CSS 文件

    我对 git 有点陌生 另外 这是我第一个自动化部署过程的项目 到目前为止 能够做到这一点真是太幸福了git push dev并上传文件 复制配置文件等 现在 当我推送到我的开发服务器时 我想缩小 JS CSS 文件 我正在考虑在服务器上安
  • 从远程获取上个月在 git 分支上提交的文件列表

    我正在尝试获取上个月在 master 分支的 github 存储库上更改的所有文件的列表 我的本地克隆没有可以追溯到那么远的日志 因此当我运行以下命令时 git diff stat 1 month ago 我收到一条警告消息和一小部分更改
  • 如何设置“阅读文档”以使 Sphinx autodoc 选项发挥作用?

    我的项目不是使用 autodoc 构建的 我遇到了关于我的项目未在 autodoc 中构建的常见问题 https read the docs readthedocs org en latest faq html my project isn
  • 返回到 Github Desktop 中的上一个提交

    我正在尝试使用 GitHub Desktop 即 GUI 应用程序 而不是命令行 返回到先前的提交 在同一分支上 我认为这是一个核心功能 因为它是首先使用源代码控制的主要原因 我可以看到可以恢复提交 但这并不是我真正想要的 因为它创建了一个
  • 我可以获取两次提交之间 git 子文件夹中已更改文件的列表吗?

    我有一个包含许多文件夹的 git 存储库 我需要找出在两次提交之间这些文件夹之一中的哪些文件发生了更改 有没有一个好的方法来做到这一点 我想你可以坚持走最后的路git diff git diff HEAD HEAD special fold
  • Windows 上的 git 忽略文件名大小写更改 [重复]

    这个问题在这里已经有答案了 我有一个reactjs应用程序 我正在将所有文件名标准化为小写以符合Nodejs 最佳实践 https devcenter heroku com articles node best practices stic
  • 使用 SourceTree 克隆存储库

    有人可以给我一个简单的使用 SourceTree 克隆存储库的快速演练吗 在书签中 我单击克隆存储库 对于源路径 我粘贴如下所示的 URL 电子邮件受保护 cdn cgi l email protection 客户端 应用程序名称 ios
  • 从 Eclipse 的历史视图中删除 ORIG_HEAD 和 FETCH_HEAD

    我最近开始使用 Eclipse Kepler 和 EGit 插件 这些分支不是我习惯的 有没有办法永久阻止这些分支的创建 我尝试手动删除它们 但它不起作用 并且我不想在下次获取或变基时保留它们 据我从对您问题的评论中了解到 您只希望这些参考
  • Azure git 部署 - 第二个程序集中缺少引用

    我正在尝试将 Bitbucket 部署设置到 Azure 网站 我成功链接了 Bitbucket 和 Azure 但是当我推送到 Bitbucket 时 我在 Azure 站点上收到以下错误 如果我单击 查看日志 它会显示以下编译错误 D
  • 在 git 中记录前 10 个

    两个问题 如何从头到尾显示 git 中的前 10 个提交 无分行 如何指定提交索引并记录它 显示第二个或第三个 我知道 git 使用父级来链接提交 很容易从头到尾记录提交 喜欢 git log HEAD 10 但我需要从头到尾查询 可以吗
  • git 从存储中删除文件

    我有一个藏匿处 里面有一堆文件 但由于文件冲突 我无法应用我的存储 我已经在我的存储中发现了有问题的文件 我想将其删除 如何从存储中删除单个文件而不破坏整个文件 存储是一次提交 或者实际上是两次甚至有时是三次提交 并且您无法更改提交 那么
  • 将主分支的提交合并到另一个分支,但不合并两个分支

    我有 git 存储库和一个主分支 我决定开发新功能 并且创建了新分支 new branch 我已经在 new branch 中创建了一些提交 但我还没有完成新功能 我决定修复 master 分支中的一些错误 因此我切换到 master 分支
  • 如何解决git中文件重命名文件夹冲突?

    我有以下问题 我有两个分支 Branch1 和 Branch2 的一些共同提交 A Branch1 是一个公共分支 位于服务器上 Branch2 是本地分支 在 Branch1 中我更改了文件 BAD folder somefile txt
  • 将远程更改合并到非当前分支的分支中

    我有多个分支 我想将远程更改合并到一个分支中不是我当前的分支 例如 git merge remote branch some other branch 仅当本地分支可以快速转发到远程头时 这才是可行的 在任何分支中 要从源获取远程分支并更新
  • 更改 Windows 安装的 Git Bash 中 ~ 目录的位置

    我什至不确定我问的是正确的问题 让我解释一下我的情况 这是关于 Windows 7 上的 Git 我的公司在网络驱动器上设置 Windows 用户目录 而不是在本地硬盘驱动器上 用于备份和超出本问题范围的其他目的 我无法改变这项政策 然而
  • 如何在Git中手动合并所有文件?

    我想合并所有文件manually有了 meld 或任何其他 diff 工具 我如何使用 Git 来做到这一点 当我跑步时git mergetool它说no files need merging 所以我想只有当我有冲突时我才能做到这一点 有更
  • Git-svn:批量删除孤立的远程分支

    我正在处理的 SVN 项目 通过 git svn 有经常创建的分支 然后与主干重新集成 然后删除 现在项目大约有10个分支没有被删除 但是在git中 gitbranch r显示大约有50个 我可以一次删除这些 检查它们是否仍然存在于 svn
  • 未签出...捆绑安装无法修复帮助!

    https github com intridea omniauth git at master is not checked out Please run bundle install Bundler GitError 那我该怎么办 捆绑
  • Mac 到 EC2 - 源代码控制三角问题 - git?同步?啥?

    我有一个日常 EC2 实例 事实上 请注意那些更改IP每次重新启动它们时 都会出现另一个问题 实例上有一个文件夹 我在文件夹 mysql express 等 中有一个简单的 比如说 节点项目 比方说 这是一个网站 那么在我的 Mac 上进行
  • 为什么 cmake 在 git commit 后编译所有内容

    假设我有时在 Linux 上使用 cmake 2 8 编译一段代码 我更改了一个文件 my changed file 运行 cmake 并且只构建了这个文件 到目前为止 一切都很好 现在我想提交这个 git add my changed f

随机推荐

  • es中修改索引名称命令_在Elasticsearch中更改索引名称

    es中修改索引名称命令 嘿 今天 我碰巧写了一个脚本来解决一个看起来很多人都面临的特定问题 重命名给定的Elasticsearch索引 自然地 有记录在案的解决方案 但是我没有Swift找到一个脚本可以让我找到我想要的位置 来自索引a所有数
  • Latex 字体加粗

    textbf w 显示为 w textbf w w
  • mysql数据库datetime字段转换成java中Date类型

    最终代码展示 输出Account类型对象 使用ResultSet类中的getDate方法只能获取到获取到日期不能得到时间 使用ResultSet类中的getTime方法只能获取到获取到时间不能得到日期 使用ResultSet类中的getTi
  • Java 开发中常见的异常有哪些?

    1 空指针异常 NullPointException 当对象不存在 却又去调用对象的属性或方法时 就会出现该异常 2 数组越界异常 ArrayIndexOutOfBoundsException 当数组只存在5个元素 他们所对应的的下标即为0
  • MySQL显示ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)解决方法

    MySQL显示ERROR 2003 HY000 Can t connect to MySQL server on localhost 10061 解决方法 第一步 在win快捷键下已管理员身份启动cmd命令然后进入mysql安装目录下的bi
  • 10信号学习之signal函数及使用其实现信号捕捉案例

    1 signal函数 功能 该函数注册一个信号捕捉函数 对比上一篇的案例和相关函数 都是针对于信号集操作的 而这个函数是针对处理动作来操作的 你可以利用此函数将捕捉到的信号按自己的方式执行 例如你可以捕捉段错误的信号后执行打印hello w
  • STM32无法下载程序

    新研发了一块STM32板 MCU使用的是STM32F4 前期硬件测试都是正常的 电源等各项硬件指标都是正常的 但是在下载程序测试的时候出现了问题 板子只能下载第一次程序成功 第二次就不能识别芯片 无法下载程序 貌似MCU被锁死 检查原理图并
  • warning: unknown attribute ‘at‘ ignored [-Wunknown-attributes] keil报错处理

    背景在KEIL v6版本编译 attribute at 语句 const unsigned char vis sensor fw attribute at 0X0800C800 0x98 0x14 0x00 0x20 0x65 0x01 0
  • java Spring调试 ApplicationContext cannot be resolved to a type

    今天开发过程中遇到了不能识别导入的Spring jar包的情况 ApplicationContext cannot be resolved to a type 本人分析情况为两种 1 导入的包错误 2 本人遇到的情况 就是jre版本过高 将
  • RawImages图片加载方式

    using UnityEngine using UnityEngine UI public class RawImagesExtend MonoBehaviour Header 资源加载方式 public SourceMode source
  • java底层学习

    额 马上就要面试了 java的底层肯定是需要了解的 网上找了找java的底层文章 做个记号 java底层主要是类的加载 连接和初始化 本文主要分为四个方面 1 java底层概述 2 new和newInstance 方法的区别 3 深入探讨j
  • activiti5之监听器

    activiti5之监听器 业务场景 在使用工作流时 通常伴随着很多具体的需求 例如 activiti人员动态的分配 当前任务节点完成的时候 指定需要指定下一个节点的处理人 比如 一个请假流程 a员工请假 需要指定下一步需要处理请假流程的领
  • Python目标检测数据集格式处理,VOC格式转YOLO格式

    众所周知 CV算法模型训练第一步该做的是数据集制作 最近遇到需要将VOC格式的数据集转为yolo格式 数据集前期的一些预处理参考博客 Python删除txt文档的某一列 fengfeng18k的博客 CSDN博客 Python修改txt某列
  • 卷积神经网络(CNN)入门:使用Python实现手写数字识别

    在上一篇文章中 我们介绍了如何使用Python实现一个简单的前馈神经网络 本文将重点介绍卷积神经网络 CNN 这是一种在计算机视觉任务中表现优异的深度学习模型 我们将从卷积神经网络的基本原理开始 介绍卷积层 池化层和全连接层等概念 然后使用
  • js实现回到顶部效果

    功能 滚动到第二屏才出现 返回顶部 按钮 点击 返回顶部 按钮会返回顶部 而且速度越来越慢 在返回顶部的途中如果用鼠标滚一下滚轮会停止返回顶部的滚动
  • python数据分析(预测性分析与机器学习)

    本文涉及到的主题如下所示 预处理 基于逻辑回归的分类 基于支持向量机的分类 基于ElasticNetCV的回归分析 支持向量回归 基于相似性传播 均值漂移算法 遗传算法 神经网络 决策树算法 1 预处理 在上一章 我们已经做过一次预处理 即
  • 复现iis7/ii7.5的fastcgi解析漏洞

    在windows server 2008 r2上搭建 php服务 1 下载php解释器 地址为http windows php net download 版本有两种 线程安全和非线程安全 线程安全是给apache用的 非线程安全是给iis用
  • 东方树叶、元气森林竞速无糖茶饮

    近几年 随着气泡水 茶饮品的横空出世 零售饮料柜的全糖时代已经渐行渐远 无糖饮料开始占据着半壁江山 据统计 在2023年推出的41款茶饮料新品中 无糖茶的创新超过6成 总计有18个品牌推出25款无糖茶新品 36种口味 所谓无糖茶 也叫原味茶
  • 【从零开始写博客】数组运用:二分查找和成员增删(day1)

    代码随想录刷题60天 目录 数组概述 array 一 数组的查找 暴力查找 枚举 二分查找 二 数组的删改 使用快慢指针对数组元素进行增删 总结 数组概述 array 数组作为一种数据结构 毫无疑问 其基本功能便是为程序员提供一种可增删查改
  • git中reset、restore、checkout、revert、clean的用法和区别

    1 git reset NAME git reset Reset current HEAD to the specified state SYNOPSIS git reset soft mixed N hard merge keep q