[Git] Detailed explanation of rollback commit version

Article directory

When the version we submitted has a bug, if it does not affect the online function, we may roll back to the previous version first. At this time, we need to use git resetthis command and the usage of its parameters: --hard, --mixed,--soft

  • Suppose there is a bug caused by our version, then we have to roll back to 还原某某文件his previous version登录功能大改
  • It should be noted that --hardafter using the rollback version, the code changes on the rollback version will be cleared.
# 查看最近 3 次的 commit 版本信息,排在最上面的就是最新的版本
# log 查看所有版本消息,-3 表示查看最近 3 次提交的版本,按 Q 键退出信息查看
➜ git:(test) git log -3
# commit:这一个版本的版本编号
# Author:作者
# Date:本次版本提交时的记录时间
commit 758a23df524dff1f11a1e53295ec5ea8d6396f7b
Author: 流星
Date:   Tue Feb 14 11:36:51 2023 +0800

    chore: 还原某某文件

commit 555a38d888d4c3743491ddeb8a4235c4ec3cc49b
Author: 流星
Date:   Tue Feb 14 11:20:15 2023 +0800

    chore: 登录功能大改

commit 6f53d8d8d4d570082e580554b68d36707f50421b
Author: 流星
Date:   Mon Feb 13 16:45:21 2023 +0800

    feat: 完善登录功能

# 根据查看到的版本commit ID,选择回退到相应的版本,这里是回退到:登录功能大改
➜ git:(test) git reset --hard 555a38d888d4c3743491ddeb8a4235c4ec3cc49b
HEAD is now at 555a38d88 chore: 登录功能大改
# 这时候再去查看版本信息,就会发现还原某某文件那个版本已经没有了
➜ git:(test) git log -2
commit 555a38d888d4c3743491ddeb8a4235c4ec3cc49b
Author: 流星
Date:   Tue Feb 14 11:20:15 2023 +0800

    chore: 登录功能大改

commit 6f53d8d8d4d570082e580554b68d36707f50421b
Author: 流星
Date:   Mon Feb 13 16:45:21 2023 +0800

    feat: 完善登录功能

(END)
  • Another common usage is:
# 不撤销 commit,会撤销 add,并删除工作空间改动代码
git reset --hard HEAD 	# 比较常用,用于清除当前改动
# 不撤销 commit,会撤销 add,不删除工作空间改动代码
git reset --mixed HEAD  # 注意用于撤销add
# 不撤销 commit,不撤销 add,不删除工作空间改动代码
git reset --soft HEAD 	# 相当于没啥用

Extension: the role of each parameter

  • --hard: Indicates ① undo commit② undo add③ delete workspace modification code
git reset --hard commitID	# commitID 指的是版本号
git reset --hard HEAD~3		# 数字大小代表的是上几个版本
git reset --hard HEAD^^^	# ^号数量代表的是上几个版本
  • --mixed: Indicates ① undo commit② undo add③ do not delete the workspace modification code
git reset --mixed commitID	# commitID 指的是版本号
git reset --mixed HEAD~3	# 数字大小代表的是上几个版本
git reset --mixed HEAD^^^	# ^号数量代表的是上几个版本
  • --soft: Indicates ① Undo commit② Don’t Undo add③ Don’t delete the workspace modification code
git reset --soft commitID	# commitID 指的是版本号
git reset --soft HEAD~3		# 数字大小代表的是上几个版本
git reset --soft HEAD^^^	# ^号数量代表的是上几个版本
  • Additional example:
# 示例:--mixed
➜ git:(test) git reset --mixed HEAD^
Unstaged changes after reset:
M       packages/pages/src/table/index.vue
➜ git:(test)git status
On branch test
Changes 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:   packages/pages/src/table/index.vue

no changes added to commit (use "git add" and/or "git commit -a")

# 示例:--soft
➜ git:(test) git reset --soft HEAD~1
➜ git:(test)git status
On branch test
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   packages/pages/src/table/index.vue

  • Detailed instructions:
git reset -h
usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
   or: git reset [-q] [<tree-ish>] [--] <pathspec>...
   or: git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]
   or: git reset --patch [<tree-ish>] [--] [<pathspec>...]
		  
    -q, --quiet           be quiet, only report errors	
    --mixed               reset HEAD and index	
    --soft                reset only HEAD	
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    --recurse-submodules[=<reset>]
                          control recursive updating of submodules
    -p, --patch           select hunks interactively
    -N, --intent-to-add   record only the fact that removed paths will be added later
    --pathspec-from-file <file>
                          read pathspec from file
    --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/129025566