
想要回滚之前的一次错误的 merge ,然而在那次 merge 之后又有几次 commit 并已 push 到远端仓库:
假设 git log 如下: commit5 10:00 commit4 9:00 commit3 8:00 wrong merge 7:00 reversion number: asdf4321 commit2 6:00 reversion number: abcd1234 commit1 5:00
回滚掉 7:00 那次 wrong merge
我的操作是 git reset --hard abcd1234 ,回到 commit2 的状态 然后再把 commit3 ,commit4 ,commit5 三个 commit 的改动...复制粘贴了回去... 再 git push -f 到远端仓库
事后被自己的行为蠢到了......
请教各位 git 圣手,这种情况如何优雅的回滚掉中间某次 merge/commit ,并保留之后的几次 commit 的代码
1 nexuszjq OP 奇怪模拟的 git log 怎么没了换行。。。 我换种写法: commit5 10:00 <- commit4 9:00 <- commit3 8:00 <- wrong merge 7:00 (reversion number: asdf4321) <- commit2 6:00 (reversion number: abcd1234) <- commit1 5:00 |
2 wolfie Sep 26, 2022 主题可以用 markdown 格式。 允许 push -f ,则借助工具用 drop commit 。 不允许就 revert 。 |
3 hsfzxjy Sep 26, 2022 via Android git rebase -i abcd1234 然后删掉 pick asdf4321 |
4 superwhite Sep 26, 2022 先 git reset,然后把后面几次的提交进行 git cherry-pick commit3,4,5... |
5 FrankAdler Sep 26, 2022 直接 revert 不行吗 |
6 baolongzhanshen Sep 26, 2022 git rebase -i abcd1234 |
7 baolongzhanshen Sep 26, 2022 @baolongzhanshen d asdf4321 |
8 renmu Sep 26, 2022 via Android 直接 revert 吧 |
9 hetal Sep 26, 2022 |
10 JasonLaw Sep 26, 2022 via iPhone |
11 youngxhui Sep 26, 2022 via Android |
12 Reficul Sep 26, 2022 $git rebase -i abcd1234 d abcd1234 :wq |
13 Jirajine Sep 26, 2022 可以用 rebase -i ,或者对小白来说最万能的办法,reset 或 checkout 到最近的“正常”commit 点,然后把后面想要的 commit 的一个个 cherry-pick 上去。 |
14 simen513 Sep 26, 2022 lazygit 有个功能,能直接将 staged 的修改应用到特定的 commit 上。后台就是用的 rebase -i 实现的,比较麻烦。 |
15 leonshaw Sep 26, 2022 git rebase --Onto=abcd1234 asdf4321 |
16 FrankHB Sep 26, 2022 不接受重写只有 revert 。 能 push -f ,那就 git replace+git filter-repo (事先保存好 config )。 简单点就 rebase ,不过不保留时间戳。 不过在此之前先学会 cherry-pick 和 format-patch/apply 吧。 |
17 foam Sep 26, 2022 revert 就是为这种场景设计的,不要搞其他的了 |
18 eraserking Sep 27, 2022 取决于你的 branch 是你自己用,还是别人也用 别人也用,为了避免别人骂你,就 revert 那个 commit 如果是自己用,revert 那个 commit 当然也行,但是如果你想直接把那个 commit 抹掉,就 rebase 到那个 commit 的前一个,然后 drop 那个 commit |