丢弃git历史的若干个提交

今天遇到一个场景:在rebase其他同事的分支时,带来了一些脏提交,然而由于同事暂时无法处理,所以需要先将这几个提交抽离出来,简单来说是这样↓

commit 我的提交5
commit 我的提交4
commit 同事的脏提交3
commit 同事的脏提交2
commit 同事的正常提交1
commit 同事的正常提交0

需要将commit 2和commit 3先抽离丢弃,等待修复后再重新rebase他的分支

其中,抽离丢弃的过程如下:

# 假设git log结果如下
commit 77df90575ec279318d224ff5bcd6f568a0d92518 (HEAD -> master)
Author: xxx
Date:   Mon Feb 17 17:28:09 2020 +0800

    我的提交5

commit 33dc720a471caaee8e3461c4becfd7b5f5cfb6dd
Author: xxx
Date:   Mon Feb 17 17:27:52 2020 +0800

    我的提交4

commit a5122cd109e83a138ea840c8b61d54ab07dd0e3c
Author: xxx
Date:   Mon Feb 17 17:27:28 2020 +0800

    脏提交3

commit 7325967966298a03f3897984fc952141f182f8d1
Author: xxx
Date:   Mon Feb 17 17:27:11 2020 +0800

    脏提交2

commit 9c583f3e86845f26892dad238294c78d8f389a40
Author: xxx
Date:   Mon Feb 17 17:26:44 2020 +0800

    正常提交1

commit ca3d77a4c1a194de677d8deb9e9742291b6a5825
Author: xxx
Date:   Mon Feb 17 17:26:20 2020 +0800

    正常提交0

选中脏提交的上一个提交的commit id,在这里对应的是“正常提交1”,即9c583f3e86845f26892dad238294c78d8f389a40,执行git rebase -i 9c583f3e86845f26892dad238294c78d8f389a40

pick 7325967 脏提交2
pick a5122cd 脏提交3
pick 33dc720 我的提交4
pick 77df905 我的提交5

# Rebase 9c583f3..77df905 onto 77df905 (4 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 <commit> = like "squash", but discard this commit's log message
# 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.
#
# Note that empty commits are commented out

根据提示 # If you remove a line here THAT COMMIT WILL BE LOST. 只需要删掉脏提交的那几行然后保存即可

# 保留以下几行,其他注释掉或删掉,保存
pick 33dc720 我的提交4
pick 77df905 我的提交5

假设没有冲突的情况下,git log会变成这样

commit 77df90575ec279318d224ff5bcd6f568a0d92518 (HEAD -> master)
Author: xxx
Date:   Mon Feb 17 17:28:09 2020 +0800

    我的提交5

commit 33dc720a471caaee8e3461c4becfd7b5f5cfb6dd
Author: xxx
Date:   Mon Feb 17 17:27:52 2020 +0800

    我的提交4

commit 9c583f3e86845f26892dad238294c78d8f389a40
Author: xxx
Date:   Mon Feb 17 17:26:44 2020 +0800

    正常提交1

commit ca3d77a4c1a194de677d8deb9e9742291b6a5825
Author: xxx
Date:   Mon Feb 17 17:26:20 2020 +0800

    正常提交0
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容