用例

$  git show --name-status 1e7cf253a6e6e
commit 1e7cf253a6e6ecee6376b12d9fcabf2ec3f7f9e6 (EH-1699)
Author: caijiachen <chajiuqqq@gmail.com>
Date:   Sat Sep 7 15:57:21 2024 +0200

    EH-1699: add other stock business interface to proto

M       oms/pkg/proto/oms_order_v3.pb.go
M       oms/pkg/proto/oms_order_v3.proto
M       rfcctl/types/mm.go
M       webproto
git fetch origin  
git reset --hard origin/dev

git stash

 git stash -h
usage: git stash list [<log-options>]
   or: git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
   or: git stash drop [-q | --quiet] [<stash>]
   or: git stash pop [--index] [-q | --quiet] [<stash>]
   or: git stash apply [--index] [-q | --quiet] [<stash>]
   or: git stash branch <branchname> [<stash>]
   or: git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
                 [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]
                 [--pathspec-from-file=<file> [--pathspec-file-nul]]
                 [--] [<pathspec>...]]
   or: git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
                 [-u | --include-untracked] [-a | --all] [<message>]
   or: git stash clear
   or: git stash create [<message>]
   or: git stash store [(-m | --message) <message>] [-q | --quiet] <commit>
$ git stash list
stash@{0}: WIP on EH-1699: 1e7cf253 EH-1699: add other stock business interface to proto
stash@{1}: On dev: A bunch of stuff that didn't work
stash@{2}: WIP on EH-1651: 88be22bf EH-1651:change sync delivery order
stash@{3}: WIP on dev: 7b2cf784 EH-1593
stash@{4}: WIP on dev: db503df4 EH-1511: debug
stash@{5}: WIP on dev: db503df4 EH-1511: debug
stash@{6}: WIP on dev: ec0e86ad Merge remote-tracking branch 'origin/dev' into dev
stash@{7}: WIP on EH-1136: 70d7b5dd oms:db update

git reset

其中 可以用 HEAD~n快捷表示。 HEAD~1表示当前指针的前一个提交。

git reset和git revert的区别

git reset:

直接修改提交历史。当你在本地进行更改且还没有推送到远程仓库时,适合使用 git reset 来修改提交历史。
通常用于清理本地开发历史或撤销误提交。
使用:git reset --hard HEAD~1

git revert:

创建一个新的提交,用来“撤销”之前某个提交的更改。当某个提交已经被推送到远程仓库,且你不想破坏历史时,应使用 git revert 来“撤销”某个提交。
在协作环境中,推荐使用 git revert,因为它不会影响其他开发者的历史。

使用:git revert <commit>

git cherry-pick

git push

git restore

用于将暂存区/某个提交的版本覆盖到工作区/暂存区。

误add了不想commit的文件到暂存区,怎么删除?

git restore --staged <文件> :将暂存区中的文件替换为仓库中某个提交的版本(默认为 HEAD)。这相当于取消暂存该文件。

也就是可以用选项指定源头和目标:
--source=<提交>: 指定恢复文件的来源提交。可以是提交哈希值、分支名或标签名。默认为 HEAD。
--staged: 指定恢复目标为暂存区。
--worktree: 指定恢复目标为工作目录。


HEAD~2和HEAD^2有什么区别
HEAD~2用于向上追溯找到前2个提交
HEAD^2用于找到第二个父提交(HEAD必须是merge节点)
image

HEAD^ 和 HEAD~ 在大多数情况下指向同一个提交

Ref