美文网首页
丢弃git历史的若干个提交

丢弃git历史的若干个提交

作者: esrever | 来源:发表于2020-02-17 17:48 被阅读0次

今天遇到一个场景:在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

相关文章

  • 丢弃git历史的若干个提交

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

  • 05_git版本回退和版本比较

    git log git的历史提交记录 commit 后面的hash值是 git提交id git log --pre...

  • 2016-07-13 Git 基础(三)

    Git 基础 - 查看提交历史 查看提交历史 git log 如果不带任何参数的话,会按照提交时间列出所有的更新。...

  • Git 基础 - 查看提交历史

    查看提交历史 git log 命令用于查看提交历史 默认不使用参数的话,git log 会按提交时间列出所有的更新...

  • Git rebase tutorial translate

    前言 Git rebase 是git的一个附加的核心功能,用于编辑提交历史。git不像有的版本控制工具对待提交历史...

  • Git- git log和git reflog的区别

    git log显示版本提交历史,显示commit的详细信息git reflog不但有版本提交历史(简要),还会显示...

  • git 回滚到之前某一commit

    git log 查看提交历史 git reset --hard HEAD引用指向给定提交,...

  • git版本控制

    查看提交历史 git log 如果想要查看git的提交历史,可以使用git log命令。本文会介绍一些常用的命令选...

  • git常用命令总结

    目录 拆pr git log 查看历史提交 git reset 回滚到提交前,但是这时候未提交文件还在 git a...

  • Git使用记录

    本地Git撤回提交记录 使用git log查看提交的历史记录 使用git reset --soft head~1撤...

网友评论

      本文标题:丢弃git历史的若干个提交

      本文链接:https://www.haomeiwen.com/subject/jtqifhtx.html