git 如何删除github中的所有提交历史?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13716658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:02:37  来源:igfitidea点击:

how to delete all commit history in github?

gitgithub

提问by Chinaxing

I want to delete all commit history but keep the code in its current state because, in my commit history, there are too many unused commits.

我想删除所有提交历史,但将代码保持在当前状态,因为在我的提交历史中,有太多未使用的提交。

How can I do it?

我该怎么做?

Is there any git command can do this?

有没有 git 命令可以做到这一点?

git filter-branch ?
git rebase ?
... 

My code is hosted on github.com.

我的代码托管在 github.com 上。

回答by Desta Haileselassie Hagos

Deleting the .gitfolder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:

删除.git文件夹可能会导致 git 存储库出现问题。如果您想删除所有提交历史记录但将代码保持在当前状态,那么这样做是非常安全的,如下所示:

  1. Checkout

    git checkout --orphan latest_branch

  2. Add all the files

    git add -A

  3. Commit the changes

    git commit -am "commit message"

  4. Delete the branch

    git branch -D master

  5. Rename the current branch to master

    git branch -m master

  6. Finally, force update your repository

    git push -f origin master

  1. 查看

    git checkout --orphan latest_branch

  2. 添加所有文件

    git add -A

  3. 提交更改

    git commit -am "commit message"

  4. 删除分支

    git branch -D master

  5. 将当前分支重命名为 master

    git branch -m master

  6. 最后,强制更新您的存储库

    git push -f origin master

PS: this will not keep your old commit history around

PS:这不会保留您旧的提交历史记录

回答by Amir Ali Akbari

If you are sure you want to remove all commit history, simply delete the .gitdirectory in your project root (note that it's hidden). Then initialize a new repository in the same folder and link it to the GitHub repository:

如果您确定要删除所有提交历史记录,只需删除.git项目根目录中的目录(注意它是隐藏的)。然后在同一个文件夹中初始化一个新的仓库,并将其链接到 GitHub 仓库:

git init
git remote add origin [email protected]:user/repo

now commit your current version of code

现在提交您当前版本的代码

git add *
git commit -am 'message'

and finally force the update to GitHub:

最后强制更新到 GitHub:

git push -f origin master

However, I suggest backing up the history (the .gitfolder in the repository) before taking these steps!

但是,我建议.git在执行这些步骤之前备份历史记录(存储库中的文件夹)!