【问题标题】:Removing all of Git history - possible? [duplicate]删除所有 Git 历史记录 - 可能吗? [复制]
【发布时间】:2014-07-07 18:27:45
【问题描述】:

我想拉下一个应用程序,删除所有 git 提交历史并重新推送它,以便整个应用程序在那里并且像以前一样正常运行,但只有一次提交,即“初始提交”。

我正在考虑这样做:

git clone https://github.com/user/user-repo.git
cd user-repo
git reset --hard tags/v2.0

rm -rf .git/
git init
git add .
git commit -m 'first commit'

git remote add stash ssh://git@myserver:7999/myproject/user-repo.git
git push --force stash master

尽管如此,我不确定这是否是实现预期结果的最佳方式。

请帮忙!

谢谢!

【问题讨论】:

  • 为什么不删除并重新创建项目?

标签: git github


【解决方案1】:

reset 命令告诉 Git 将当前“位置”移回某个提交(在本例中为您的第一次提交),并添加 --soft 标志告诉 Git 将所有文件保持原样你最近的提交:

git reset <first commit> --soft

然后您可以提交您所做的所有更改,修改之前的(初始)提交(您在后续提交中所做的更改应该已经为您暂存):

git commit --amend -m "Initial commit"

【讨论】:

    【解决方案2】:

    您可以在不删除.git/ 目录的情况下:

    git rebase -i <sha hash of the very first commit>
    

    然后,您将按自上而下的顺序列出所有应用的提交。将每个pick 更改为squash,每个提交都将被压缩到前一个,这应该只剩下一个提交。

    【讨论】:

    • 谢谢卡切!因此,即它将是 git rebase -i d9fcfde2b26d9d3b7a6b89a0fe78ee05b0db20cf (初始提交)。这会将我后面的所有提交压缩到初始提交中?
    • Rebase 开始一个将重新应用提交的操作。如果你什么都不做,它只会像往常一样重新应用所有提交,这是你不想要的。我将编辑我的答案以使其更清楚。无论如何,这个问题似乎已经在别处得到了相当清楚的回答——您应该阅读该问题,以便更完整、更明确地了解您的选择。
    【解决方案3】:

    您可以创建一个新的孤立分支(即没有父分支的分支)并将主分支设置为:

    $ git checkout --orphan new-master
    $ git commit
    $ git checkout master
    $ git reset --hard new-master
    $ git branch -d new-master
    

    【讨论】:

      【解决方案4】:

      您正在尝试的是删除历史记录的最明确方法 - 删除 .git 并创建一个全新的 repo 并强制推送。它应该可以工作。

      您也可以这样做(更保守)的方法:

      git checkout --orphan temp
      git add -A
      git commit
      git branch -D master
      git branch -m master
      git push -f origin master
      

      创建一个孤立分支(无父分支),将当前工作副本文件添加到其中并提交。删除 master 分支,并将孤立分支重命名为 master。强制推送新的master。

      【讨论】:

        猜你喜欢
        • 2016-02-24
        • 1970-01-01
        • 2021-05-06
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 2012-12-05
        • 2020-03-22
        • 2015-06-04
        相关资源
        最近更新 更多