玖叶教程网

前端编程开发入门

Git 删除某一次提交或Merge(git删除某一次commit)

Git 删除某一次提交或Merge

Git 删除某次 commit

一、 git reset

  • git reset :回滚到某次提交。
  • git reset –soft:此次提交之后的修改会被退回到暂存区
  • git reset –hard:此次提交之后的修改不做任何保留,git status 查看工作区是没有记录的。

前置准备:

FrankLee@DESKTOP MINGW64 /d/users/Desktop/test2 (master)
$ ll
total 4
-rw-r--r-- 1 FrankLee 197121 6 Nov  3 18:14 a.txt
-rw-r--r-- 1 FrankLee 197121 3 Nov  3 18:15 b.txt
-rw-r--r-- 1 FrankLee 197121 2 Nov  3 18:15 c.txt
-rw-r--r-- 1 FrankLee 197121 2 Nov  3 18:15 d.txt

FrankLee@DESKTOP MINGW64 /d/users/Desktop/test2 (master)
$ git status
On branch master
nothing to commit, working tree clean

Demo1. 回滚代码 如果需要删除的 commit 是最新的,那么可以通过 git reset 命令将代码回滚到之前某次提交的状态,但一定要将现有的代码做好备份,否则回滚之后这些变动都会消失。具体操作如下:

  1. git log // 查询要回滚的 commit_id ```cpp commit fea033b17af24f44fe2303997d53a5432eedbaf7 (HEAD -> master) Author: Frank Lee Date: Wed Nov 3 18:16:00 2021 +0800
  2. 5

commit 6496ef00f556544386c0d518c29645b4aba6b52d Merge: 818f852 a061a93 Author: Frank Lee Date: Wed Nov 3 18:15:30 2021 +0800

Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c Author: Frank Lee Date: Wed Nov 3 18:15:09 2021 +0800

4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop) Author: Frank Lee Date: Wed Nov 3 18:14:35 2021 +0800

2. git reset --hard commit_id // HEAD 就会指向此次的提交记录
```shell
git reset --hard 6496ef00f556544386c0d518c29645b4aba6b52d

3.查看结果:

FrankLee@DESKTOP MINGW64 /d/users/Desktop/test2 (master)
$ git log
commit 6496ef00f556544386c0d518c29645b4aba6b52d (HEAD -> master)
Merge: 818f852 a061a93
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:13:26 2021 +0800
  1. git push origin HEAD –force // 强制推送到远端 git push origin HEAD --force

Demo2. 误删恢复 如果回滚代码之后发现复制错了 commit_id,或者误删了某次 commit 记录,也可以通过下方代码恢复:

  1. git reflog // 复制要恢复操作的前面的 hash 值 ``` 6496ef0 (HEAD -> master) HEAD@{0}: reset: moving to 6496ef00f556544386c0d518c29645b4aba6b52d fea033b HEAD@{1}: commit: 5 6496ef0 (HEAD -> master) HEAD@{2}: merge develop: Merge made by the ‘recursive’ strategy. 818f852 HEAD@{3}: commit: 4 98d66bd HEAD@{4}: checkout: moving from develop to master a061a93 (develop) HEAD@{5}: commit: 3 7b215fa HEAD@{6}: checkout: moving from master to develop 98d66bd HEAD@{7}: commit: 2 7b215fa HEAD@{8}: commit (initial): 1
2. git reset --hard hash // 将 hash 换成要恢复的历史记录的 hash 值

git reset –hard fea033b


1. 注意:删除中间某次提交时最好不要用 git reset 回退远程库,因为之后其他人提交代码时用 git pull 也会把自己的本地仓库回退到之前的版本,容易出现差错进而增加不必要的工作量。

#### 二、git rebase

- git rebase:当两个分支不在一条线上,需要执行 merge 操作时使用该命令。

Demo1. 撤销提交
   如果中间的某次 commit 需要删除,可以通过 git rebase 命令实现,方法如下:


1. git log // 查找要删除的前一次提交的 commit_id

```shell
$ git log
commit fea033b17af24f44fe2303997d53a5432eedbaf7 (HEAD -> master)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 6496ef00f556544386c0d518c29645b4aba6b52d
Merge: 818f852 a061a93
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:13:26 2021 +0800

    2

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:12:47 2021 +0800

    1

  1. git rebase -i commit_id // 将 commit_id 替换成复制的值
git rebase -i 98d66bdf83596672477186d2c9a2135697728fb9

pick 818f852 4
pick a061a93 3
pick fea033b 5

# Rebase 98d66bd..fea033b onto 98d66bd (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified); use -c <commit> to reword the commit message
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
~
~
~

  1. 进入 Vim 编辑模式,将要删除的 commit 前面的 pick 改成 drop
drop 818f852 4
pick a061a93 3
pick fea033b 5

# Rebase 98d66bd..fea033b onto 98d66bd (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified); use -c <commit> to reword the commit message
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
  1. 保存并退出 Vim
$ git rebase -i 98d66bdf83596672477186d2c9a2135697728fb9
Successfully rebased and updated refs/heads/master.


$ git log
commit 06b518eaff4afbe9f719c5e0933267cd310c03fc (HEAD -> master)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 3c0821169f41d74b263aa9ad4bb465a93b734e2d
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:13:26 2021 +0800

    2

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:12:47 2021 +0800

    1

$ git checkout develop
Switched to branch 'develop'

FrankLee@DESKTOP-RQ609Q2 MINGW64 /d/users/Desktop/test2 (develop)
$ ll
total 2
-rw-r--r-- 1 FrankLee 197121 3 Nov  3 19:23 a.txt
-rw-r--r-- 1 FrankLee 197121 3 Nov  3 19:21 b.txt

FrankLee@DESKTOP-RQ609Q2 MINGW64 /d/users/Desktop/test2 (develop)
$ git log
commit a061a9334edeef73176e9a846d4e369a5e31bb86 (HEAD -> develop)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:12:47 2021 +0800

    1

这样就完成了。

  1. 解决冲突 该命令执行时极有可能出现 reabase 冲突,可以通过以下方法解决:
1. git diff // 查看冲突内容
2. // 手动解决冲突(冲突位置已在文件中标明)
3. git add <file> 或 git add -A // 添加
4. git rebase --continue // 继续 rebase
5. // 若还在 rebase 状态,则重复 2、3、4,直至 rebase 完成出现 applying 字样
6. git push

三、 git revert

  • git revert:放弃某次提交。 git revert 之前的提交仍会保留在 git log 中,而此次撤销会做为一次新的提交。
  • git revert -m:用于对 merge 节点的操作,-m 指定具体某个提交点。

Demo1. 撤销提交 要撤销中间某次提交时,使用 git revert 也是一个很好的选择:

  1. git log // 查找需要撤销的 commit_id
$ git log
commit fea033b17af24f44fe2303997d53a5432eedbaf7 (HEAD -> master)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 6496ef00f556544386c0d518c29645b4aba6b52d
Merge: 818f852 a061a93
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:14:35 2021 +0800
...
  1. git revert commit_id // 撤销这次提交
$ git revert 818f852eb63b978491fa5afdccd7aefe4f745a9c
Removing c.txt
[master 32ee39e] Revert "4"
 1 file changed, 1 deletion(-)
 delete mode 100644 c.txt

$ git log
commit 32ee39e4d571b11f421ec114a7e884b8369df97b (HEAD -> master)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 19:04:30 2021 +0800

    Revert "4"

    This reverts commit 818f852eb63b978491fa5afdccd7aefe4f745a9c.

commit fea033b17af24f44fe2303997d53a5432eedbaf7
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 6496ef00f556544386c0d518c29645b4aba6b52d
Merge: 818f852 a061a93
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:13:26 2021 +0800

    2

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <[email protected]>
Date:   Wed Nov 3 18:12:47 2021 +0800

    1

Demo2. 撤销 merge 节点提交 如果这次提交是 merge 节点的话,则需要加上 -m 指令:

  1. git revert commit_id -m 1 // 第一个提交点
 $ git revert 6496ef00f556544386c0d518c29645b4aba6b52d -m 1
Removing b.txt
[master fce4cf4] Revert "Merge branch 'develop'"
 1 file changed, 1 deletion(-)
 delete mode 100644 b.txt
 
 $ git reflog
fce4cf4 (HEAD -> master) HEAD@{0}: revert: Revert "Merge branch 'develop'"
fea033b HEAD@{1}: commit: 5
6496ef0 HEAD@{2}: merge develop: Merge made by the 'recursive' strategy.
818f852 HEAD@{3}: commit: 4
98d66bd HEAD@{4}: checkout: moving from develop to master
a061a93 (develop) HEAD@{5}: commit: 3
7b215fa HEAD@{6}: checkout: moving from master to develop
98d66bd HEAD@{7}: commit: 2
7b215fa HEAD@{8}: commit (initial): 1


2. // 如果有冲突,手动解决冲突
3. git add -A
4. git commit -m ""
5. git revert commit_id -m 2 // 第二个提交点
6. // 重复 2,3,4
7. git push

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言