龙空技术网

Git 删除某一次提交或Merge

超越永无止境技有所长 1795

前言:

眼前同学们对“centos卸载git263”大体比较关心,我们都想要知道一些“centos卸载git263”的相关文章。那么小编也在网络上网罗了一些有关“centos卸载git263””的相关内容,希望各位老铁们能喜欢,你们一起来了解一下吧!

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

前置准备:

FrankLee@DESKTOP MINGW64 /d/users/Desktop/test2 (master)$ lltotal 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.txtFrankLee@DESKTOP MINGW64 /d/users/Desktop/test2 (master)$ git statusOn branch masternothing to commit, working tree clean

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

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

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 就会指向此次的提交记录```shellgit reset --hard 6496ef00f556544386c0d518c29645b4aba6b52d

3.查看结果:

FrankLee@DESKTOP MINGW64 /d/users/Desktop/test2 (master)$ git logcommit 6496ef00f556544386c0d518c29645b4aba6b52d (HEAD -> master)Merge: 818f852 a061a93Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:30 2021 +0800    Merge branch 'develop'commit 818f852eb63b978491fa5afdccd7aefe4f745a9cAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:09 2021 +0800    4commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:14:35 2021 +0800    3commit 98d66bdf83596672477186d2c9a2135697728fb9Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:13:26 2021 +0800
git push origin HEAD –force // 强制推送到远端 git push origin HEAD --force

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

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 logcommit fea033b17af24f44fe2303997d53a5432eedbaf7 (HEAD -> master)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:16:00 2021 +0800    5commit 6496ef00f556544386c0d518c29645b4aba6b52dMerge: 818f852 a061a93Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:30 2021 +0800    Merge branch 'develop'commit 818f852eb63b978491fa5afdccd7aefe4f745a9cAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:09 2021 +0800    4commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:14:35 2021 +0800    3commit 98d66bdf83596672477186d2c9a2135697728fb9Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:13:26 2021 +0800    2commit 7b215fa6d0b4bbebb245edc31ef2457776d5248aAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:12:47 2021 +0800    1
git rebase -i commit_id // 将 commit_id 替换成复制的值
git rebase -i 98d66bdf83596672477186d2c9a2135697728fb9pick 818f852 4pick a061a93 3pick 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.#~~~
进入 Vim 编辑模式,将要删除的 commit 前面的 pick 改成 drop
drop 818f852 4pick a061a93 3pick 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.#
保存并退出 Vim
$ git rebase -i 98d66bdf83596672477186d2c9a2135697728fb9Successfully rebased and updated refs/heads/master.$ git logcommit 06b518eaff4afbe9f719c5e0933267cd310c03fc (HEAD -> master)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:16:00 2021 +0800    5commit 3c0821169f41d74b263aa9ad4bb465a93b734e2dAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:14:35 2021 +0800    3commit 98d66bdf83596672477186d2c9a2135697728fb9Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:13:26 2021 +0800    2commit 7b215fa6d0b4bbebb245edc31ef2457776d5248aAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:12:47 2021 +0800    1$ git checkout developSwitched to branch 'develop'FrankLee@DESKTOP-RQ609Q2 MINGW64 /d/users/Desktop/test2 (develop)$ lltotal 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.txtFrankLee@DESKTOP-RQ609Q2 MINGW64 /d/users/Desktop/test2 (develop)$ git logcommit a061a9334edeef73176e9a846d4e369a5e31bb86 (HEAD -> develop)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:14:35 2021 +0800    3commit 7b215fa6d0b4bbebb245edc31ef2457776d5248aAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:12:47 2021 +0800    1

这样就完成了。

解决冲突 该命令执行时极有可能出现 reabase 冲突,可以通过以下方法解决:

1. git diff // 查看冲突内容2. // 手动解决冲突(冲突位置已在文件中标明)3. git add <file> 或 git add -A // 添加4. git rebase --continue // 继续 rebase5. // 若还在 rebase 状态,则重复 2、3、4,直至 rebase 完成出现 applying 字样6. git push
三、 git revertgit revert:放弃某次提交。 git revert 之前的提交仍会保留在 git log 中,而此次撤销会做为一次新的提交。git revert -m:用于对 merge 节点的操作,-m 指定具体某个提交点。

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

git log // 查找需要撤销的 commit_id

$ git logcommit fea033b17af24f44fe2303997d53a5432eedbaf7 (HEAD -> master)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:16:00 2021 +0800    5commit 6496ef00f556544386c0d518c29645b4aba6b52dMerge: 818f852 a061a93Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:30 2021 +0800    Merge branch 'develop'commit 818f852eb63b978491fa5afdccd7aefe4f745a9cAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:09 2021 +0800    4commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:14:35 2021 +0800...
git revert commit_id // 撤销这次提交
$ git revert 818f852eb63b978491fa5afdccd7aefe4f745a9cRemoving c.txt[master 32ee39e] Revert "4" 1 file changed, 1 deletion(-) delete mode 100644 c.txt$ git logcommit 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 fea033b17af24f44fe2303997d53a5432eedbaf7Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:16:00 2021 +0800    5commit 6496ef00f556544386c0d518c29645b4aba6b52dMerge: 818f852 a061a93Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:30 2021 +0800    Merge branch 'develop'commit 818f852eb63b978491fa5afdccd7aefe4f745a9cAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:15:09 2021 +0800    4commit a061a9334edeef73176e9a846d4e369a5e31bb86 (develop)Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:14:35 2021 +0800    3commit 98d66bdf83596672477186d2c9a2135697728fb9Author: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:13:26 2021 +0800    2commit 7b215fa6d0b4bbebb245edc31ef2457776d5248aAuthor: Frank Lee <surpass_li@aliyun.com>Date:   Wed Nov 3 18:12:47 2021 +0800    1

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

git revert commit_id -m 1 // 第一个提交点

 $ git revert 6496ef00f556544386c0d518c29645b4aba6b52d -m 1Removing b.txt[master fce4cf4] Revert "Merge branch 'develop'" 1 file changed, 1 deletion(-) delete mode 100644 b.txt  $ git reflogfce4cf4 (HEAD -> master) HEAD@{0}: revert: Revert "Merge branch 'develop'"fea033b HEAD@{1}: commit: 56496ef0 HEAD@{2}: merge develop: Merge made by the 'recursive' strategy.818f852 HEAD@{3}: commit: 498d66bd HEAD@{4}: checkout: moving from develop to mastera061a93 (develop) HEAD@{5}: commit: 37b215fa HEAD@{6}: checkout: moving from master to develop98d66bd HEAD@{7}: commit: 27b215fa HEAD@{8}: commit (initial): 12. // 如果有冲突,手动解决冲突3. git add -A4. git commit -m ""5. git revert commit_id -m 2 // 第二个提交点6. // 重复 2,3,47. git push

标签: #centos卸载git263