关山难越,谁悲失路之人;萍水相逢,尽是他乡之客。
百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程教程 > 技术文章 > 正文

Git 删除某一次提交或Merge

guanshanw 2023-10-07 12:07 35 浏览 0 评论

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 命令将代码回滚到之前某次提交的状态,但一定要将现有的代码做好备份,否则回滚之后这些变动都会消失。具体操作如下:

Git 删除某一次提交或Merge

  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 <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <surpass_li@aliyun.com>
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 <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 6496ef00f556544386c0d518c29645b4aba6b52d
Merge: 818f852 a061a93
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:13:26 2021 +0800

    2

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <surpass_li@aliyun.com>
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 <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 3c0821169f41d74b263aa9ad4bb465a93b734e2d
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:13:26 2021 +0800

    2

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <surpass_li@aliyun.com>
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 <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <surpass_li@aliyun.com>
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 <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 6496ef00f556544386c0d518c29645b4aba6b52d
Merge: 818f852 a061a93
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <surpass_li@aliyun.com>
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 <surpass_li@aliyun.com>
Date:   Wed Nov 3 19:04:30 2021 +0800

    Revert "4"

    This reverts commit 818f852eb63b978491fa5afdccd7aefe4f745a9c.

commit fea033b17af24f44fe2303997d53a5432eedbaf7
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:16:00 2021 +0800

    5

commit 6496ef00f556544386c0d518c29645b4aba6b52d
Merge: 818f852 a061a93
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:30 2021 +0800

    Merge branch 'develop'

commit 818f852eb63b978491fa5afdccd7aefe4f745a9c
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:15:09 2021 +0800

    4

commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:14:35 2021 +0800

    3

commit 98d66bdf83596672477186d2c9a2135697728fb9
Author: Frank Lee <surpass_li@aliyun.com>
Date:   Wed Nov 3 18:13:26 2021 +0800

    2

commit 7b215fa6d0b4bbebb245edc31ef2457776d5248a
Author: Frank Lee <surpass_li@aliyun.com>
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

相关推荐

七条简单命令让您玩转Git
七条简单命令让您玩转Git

凭借着出色的协作能力、快速部署效果与代码构建辅助作用,Git已经得到越来越多企业用户的青睐。除了用于开发商业及消费级应用之外,众多科学及政府机构也开始尝试使用这...

2023-10-07 12:14 guanshanw

基本完整的关于Git分支branch的操作
基本完整的关于Git分支branch的操作

Git使用背景项目中要用到dev或者其他分支开发完代码,需要将该分支合并到master的需求操作步骤下面以dev名称为lex为分支名为例来操作一遍客户端操作:...

2023-10-07 12:14 guanshanw

Git 进阶(合并与变基)
Git 进阶(合并与变基)

在Git中整合来自不同分支的修改主要有两种方法:合并(merge)以及变基(rebase)合并(merge)merge流程图merge的原理是找到这两个分...

2023-10-07 12:13 guanshanw

Git学习笔记 003 Git进阶功能 part5 合并(第一部分)

合并(merge)是很常用的操作。尤其是一个庞大的很多人参与开发的企业级应用。一般会设定一个主分支,和多个副分支。在副分支开发完成后,合并到主分支中。始终保持主分支是一个完整的,稳定的最新状态的分支。...

非标题党,三张图帮你理解git merge和git rebase的区别
非标题党,三张图帮你理解git merge和git rebase的区别

初始场景:基于正常的开发分支修改几个小bug,然后在合并到开发分支上。gitmergegitcheckoutfeaturegitmergeho...

2023-10-07 12:13 guanshanw

git 初次使用(01)
git 初次使用(01)

先从github上克隆代码下来:使用vscode克隆代码如下图,填写上github仓库地址:vscode有时候克隆代码速度比较慢,可以用命令行方式克隆gitc...

2023-10-07 12:12 guanshanw

Git 远程操作

4.Git远程操作命令说明gitremote远程版本库操作gitfetch从远程获取版本库gitpull下载远程代码并合并gitpush上传远程代码并合并4.1远程版本库操作gitre...

Git常用命令-总结
Git常用命令-总结

创建git用户$gitconfig--globaluser.name"YourName"$gitconfig--globaluser.em...

2023-10-07 12:12 guanshanw

git中删除从别人clone下来项目的git信息,并修改为自己的分支

如果你从别人的Git存储库中克隆了一个项目,并想要删除与该存储库相关的Git信息,并将其修改为你自己的分支,则可以执行以下步骤:使用gitclone命令克隆存储库:gitclone<u...

git系列-回滚和放弃本地修改

回滚历史提交就是reset的功能。这种情况是已经提交远程仓库,需要回滚到之前的提交。gitreset--hardcommitId//注:强制提交后,当前版本后面的提交版本将会删掉!gi...

GIT使用小技巧大全
GIT使用小技巧大全

在大型软件工程的开发过程中,版本控制是无法绕过去的;目前来说,最火的版本控制软件就是GIT了。早两年SVN比较火,不过被大神linus喷了几次后,就日落西山了,...

2023-10-07 12:11 guanshanw

git相关命令-上
git相关命令-上

这些命令都是看了文档后,个人觉得比较有用的一些,展示给大家。回到远程仓库的状态抛弃本地所有的修改,回到远程仓库的状态。gitfetch--all&...

2023-10-07 12:10 guanshanw

Git命令行接口:掌握Git的必备技能
Git命令行接口:掌握Git的必备技能

Git是一款强大的分布式版本控制工具,它支持命令行界面操作。熟练掌握Git命令行接口,是开发者使用Git的必备技能之一。在这篇文章中,我们将介绍Git命令行接口...

2023-10-07 12:10 guanshanw

Git命令详解
Git命令详解

相信各位小伙伴们应该都对git有一些了解,毕竟作为代码管理的神器,就算不是IT行业的小伙伴肯定也或多或少的听说过一些。今天就来和小伙伴们分享一下自己总结的常用命...

2023-10-07 12:10 guanshanw

工作7年收集到的git命令
工作7年收集到的git命令

概念git中的术语解释:仓库也叫版本库(repository)stage:暂存区,add后会存到暂存区,commit后提交到版本库git安装linux...

2023-10-07 12:10 guanshanw

取消回复欢迎 发表评论: