丢弃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  = use commit
# r, reword  = use commit, but edit the commit message
# e, edit  = use commit, but stop for amending
# s, squash  = use commit, but meld into previous commit
# f, fixup  = like "squash", but discard this commit's log message
# x, exec  = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop  = remove commit
# l, label 

根据提示 # 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

你可能感兴趣的:(丢弃git历史的若干个提交)