git reset --soft,--hard的区别 与 git revert与git-cherry-pick

Git恢复之前版本的两种方法reset、revert(图文详解)

命令行: git reset --help 和 git revert --help 即可查看官方文档

git-reset

Reset current HEAD to the specified state

git reset的作用是修改HEAD的位置,即将HEAD指向的位置改变为之前存在的某个版本

适用场景: 如果想恢复到之前某个提交的版本,且那个版本之后提交的版本我们都不要了,就可以用这种方法。

git-revert

Revert some existing commits

git revert是用于“反做”某一个版本,以达到撤销该版本的修改的目的。
比如,我们commit了三个版本(版本一、版本二、 版本三),突然发现版本二不行(如:有bug),
想要撤销版本二,但又不想影响撤销版本三的提交,就可以用 git revert 命令来反做版本二,
生成新的版本四,这个版本四里会保留版本三的东西,但撤销了版本二的东西。

适用场景: 如果我们想撤销之前的某一版本,但是又想保留该目标版本后面的版本,记录下这整个版本变动流程,就可以用这种方法。

git reset

git reset --soft,–hard的区别

  1. 添加一个文件:git【未追踪】为【红色】,
  2. 【add】:变【蓝色(index file)】,
  3. 【commit】:变【黑色(working tree: 即当前的工作目录,tree of commit)】
git reset [<mode>] [<commit>]

This form resets the current branch head to and possibly updates the index (resetting it to the tree of )
and the working tree depending on . If is omitted, defaults to --mixed. The must be one of the following:

此表单将当前分支头重置为,并可能根据更新索引(将其重置为的树)和工作树。如果省略,则默认为–mixed。 必须是以下之一:

–soft : 蓝色(倒退3,已add)

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

完全不触摸索引文件或工作树(而是将头重置为,就像所有模式一样)。就像git status那样,这会将所有更改的文件保留为“要提交的更改”。

–mixed : 红色(倒退32 , 未add)

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 : 全删(倒退321 , 文件已完全不存在)

Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

重置索引和工作树。自以来,工作树中跟踪文件的任何更改都将被放弃。

–merge (不常用)

Resets the index and updates the files in the working tree that are different between 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 and the index has unstaged changes, reset is aborted.

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

重置索引并更新工作树中和HEAD之间不同的文件,但保留那些索引和工作树中不同的文件(即,尚未添加的更改)。
如果与索引之间的文件不同,则未进行暂存更改,则重置将中止。
换句话说,–merge类似于git read-tree -u -m 一样,但是会携带未合并的索引条目。

–keep (不常用)

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

重置索引条目并更新工作树中和HEAD之间不同的文件。如果和HEAD之间不同的文件具有本地更改,则重置将中止。

git revert

git revert 的操作

使用 git revert 可以撤销指定的提交, 要撤销一串提交可以用 … 语法。 注意这是一个前开后闭区间,即不包括 commit1,但包括 commit2。

注意 revert 命令会对每个撤销的 commit 进行一次提交,–no-commit 后可以最后一起手动提交。

//revert
git revert e7a957858c44e9ff9b785697af043930ec910e5c..9c066edaeff4e01ed50fe09c62635ca0b05eba81

//查看不同版本
git diff e7a957858c44e9ff9b785697af043930ec910e5c..9c066edaeff4e01ed50fe09c62635ca0b05eba81

git-cherry-pick

参考:[Git] Git整理(五) git cherry-pick的使用

Apply the changes introduced by some existing commits

应用一些现有提交引入的更改

git cherry-pick可以理解为”挑拣”提交,它会获取某一个分支的单笔提交,并作为一个新的提交引入到你当前分支上。
当我们需要在本地合入其他分支的提交时,如果我们不想对整个分支进行合并,而是只想将某一次提交合入到本地当前分支上,那么就要使用git cherry-pick了。

简而言之,就是a分支的提交a1,a2,a3,b分支不想全部合并a,只想合并a2,就可以使用cherry-pick
切换到分支b,使用命令 git cherry-pick a2

用法
git cherry-pick [<options>] <commit-ish>...

常用options:
    --quit                退出当前的chery-pick序列
    --continue            继续当前的chery-pick序列
    --abort               取消当前的chery-pick序列,恢复当前分支
    -n, --no-commit       不自动提交
    -e, --edit            编辑提交信息
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值