暂存区撤销
git reset HEAD <file>...
撤销工作目录修改
git checkout -- <file>...
git reset PK git revert
git revert
git revert撤销某次提交,但是被撤销的提交是会被保留的。
如下:
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
bbe630da410cda6fa910689cb92710309c12b540 commit3
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
62731c3c0bdfd2f169cff665d7cad6d3602718f2 commit1
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git revert HEAD
[wgh_dev f601569] Revert "commit3"
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 wgh3.h
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git status
# On branch wgh_dev
nothing to commit (working directory clean)
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
f601569f11d3f5cf583e8a989b067e138e9287d7 Revert "commit3"
bbe630da410cda6fa910689cb92710309c12b540 commit3
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git reset --soft HEAD~1
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev) //--soft为默认值,被退回的修改会退到暂存区。
$ git status
# On branch wgh_dev
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: wgh3.h
#
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
bbe630da410cda6fa910689cb92710309c12b540 commit3 //可以看到没有添加一个类似 Revert "commit3"的提交,而是直接退到了commit3
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
62731c3c0bdfd2f169cff665d7cad6d3602718f2 commit1
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev )//直接退回到了commit2,被退回的修改不会退到暂存区。
$ git reset --hard HEAD~1
HEAD is now at 0ff91e7 commit2
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
62731c3c0bdfd2f169cff665d7cad6d3602718f2 commit1
0df764e62b09176095cba24013be4cc3b0733b94 Merge branch 'TEST_main_branch' of d:/g
$ git log -3 --pretty=oneline
bbe630da410cda6fa910689cb92710309c12b540 commit3
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
62731c3c0bdfd2f169cff665d7cad6d3602718f2 commit1
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git revert HEAD
[wgh_dev f601569] Revert "commit3"
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 wgh3.h
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git status
# On branch wgh_dev
nothing to commit (working directory clean)
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
f601569f11d3f5cf583e8a989b067e138e9287d7 Revert "commit3"
bbe630da410cda6fa910689cb92710309c12b540 commit3
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
git reset
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git reset --soft HEAD~1
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev) //--soft为默认值,被退回的修改会退到暂存区。
$ git status
# On branch wgh_dev
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: wgh3.h
#
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
bbe630da410cda6fa910689cb92710309c12b540 commit3 //可以看到没有添加一个类似 Revert "commit3"的提交,而是直接退到了commit3
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
62731c3c0bdfd2f169cff665d7cad6d3602718f2 commit1
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev )//直接退回到了commit2,被退回的修改不会退到暂存区。
$ git reset --hard HEAD~1
HEAD is now at 0ff91e7 commit2
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git log -3 --pretty=oneline
0ff91e7df56e4505a012acb35cd2fe5bb184b24d commit2
62731c3c0bdfd2f169cff665d7cad6d3602718f2 commit1
0df764e62b09176095cba24013be4cc3b0733b94 Merge branch 'TEST_main_branch' of d:/g
修改提交
git commit --amend
修改最近一次提交。
修改commit 注释或者补充漏掉的commit的文件。
King@ROYAL /d/gitworkspace/worktree/test (wgh_dev)
$ git commit --amend