Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git(集线器)删除旧的提交_Git_Github_Commit - Fatal编程技术网

Git(集线器)删除旧的提交

Git(集线器)删除旧的提交,git,github,commit,Git,Github,Commit,我做了一些我不想再做的垃圾承诺 比如说,我做了一些小的更改,并在每次更改后提交。但我只想从上次提交到github。我的历史是这样的: Initial commit --> Second commit --> Third commit --> Fourth commit 只有我的第四次提交足够好,可以推送到github,那么我如何删除提交1到3,但保留4 我试图用谷歌搜索我的问题,但我只能找到如何删除最新提交或类似的内容。使用git revert,其中是git日志中显示的提交的

我做了一些我不想再做的垃圾承诺

比如说,我做了一些小的更改,并在每次更改后提交。但我只想从上次提交到github。我的历史是这样的:

Initial commit --> Second commit --> Third commit --> Fourth commit
只有我的第四次提交足够好,可以推送到github,那么我如何删除提交1到3,但保留4

我试图用谷歌搜索我的问题,但我只能找到如何删除最新提交或类似的内容。

使用
git revert
,其中
是git日志中显示的提交的哈希。基本上,它将创建另一个提交,该提交将还原在指定提交中所做的更改。 以下是步骤:

  • git日志
    并注意commit的哈希值
    1-3
  • git revert
    git revert
    git revert
  • 现在您将有额外的
    三次提交
    ,这将恢复提交
    1-3的更改
  • git-rebase-i HEAD~7
    这会将您的7次提交压缩为1次
  • 出现提示时,编辑
    commit 4
    ,将其提交消息作为
    pick
    推送,并将其他消息更改为
    squash

  • 有关详细信息,请参见

    如果最新提交依赖于最旧的提交,则无法删除它们。如果没有:

    git rebase -i HEAD~4
    
    并通过删除相应的行来删除不需要的提交