使用Git重新开始,并将仓库的最后一次提交作为唯一的提交。

4
我有一个包含数十个提交的 Git 代码库。 我希望将最后一次提交作为唯一的提交 - 这意味着该代码库的行为就像刚初始化一样,当前文件状态是唯一的提交。
是否有更好的方法来实现这个目标,而不是创建一个新的代码库?

没有其他方法可以实现这个功能,而不是创建一个新的仓库。rm -rf .git; git init . 看起来更简单,尽管你会丢失配置。 - millimoose
@millimoose,我想你是对的,这样做更简单。如果你把它作为答案,我会点赞的。 - user456814
重复的问题:Git squash all commits into a single commit - user456814
2个回答

11
使用以下内容:
$ git checkout --orphan new-master master
$ git commit -m "Initial commit for new root"

# Compare new-master with master to double-check that
# their final commit states are identical:
$ git diff new-master master

# If there is no diff/patch output, then they're identical,
# so it's safe to delete master:
$ git branch -D master
$ git branch --move new-master master
--orphan标志创建了一个完全独立的提交树,具有不同的根提交。它遵循基本的分支语法,所以后面跟着的参数是新分支的名称,然后是起点,可以是任何Git修订规范,包括提交sha或分支名称。
因此,在第一条命令中,您告诉Git使用与master分支当前状态相同的状态创建一个新的孤立分支。然后,您需要像第一次使用原始根一样提交新根。
接下来,您想确保new-master的最终状态确实与master的最终提交状态相同,因此请对它们进行差异比较以确保(没有差异输出意味着它们相同)。然后进行清理,删除旧的master分支并将新分支重命名为它。
以下是来自官方Linux内核Git文档git-checkout(1)--orphan标志的解释(第一行由我进行了语法修正):
--orphan <new_branch> [<start_point>]

Create a new orphan branch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

The index and the working tree are adjusted as if you had previously run git checkout <start_point>. This allows you to start a new history that records a set of paths similar to <start_point> by easily running git commit -a to make the root commit.

This can be useful when you want to publish the tree from a commit without exposing its full history. You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code.

If you want to start a disconnected history that records a set of paths that is totally different from the one of <start_point>, then you should clear the index and the working tree right after creating the orphan branch by running "git rm -rf ." from the top level of the working tree. Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc.


一切都进行得很顺利,直到我尝试将代码推送回Github时,出现了“提示:更新被拒绝,因为您当前分支的最新提交落后于远程仓库”的错误。 - Guy
好的 - 我读了一些内容并尝试了 "git push origin :master" - 差不多成功了(在Github上删除是被禁止的...) - Guy
@Guy 你通常需要强制推送以重写历史记录。这样做的原因是,如果有其他人曾经检出了你的存储库,那么这是一个极其糟糕的想法。就任何其他关联存储库而言,你所做的与删除GitHub存储库并启动新存储库的操作几乎是无法区分的。 - millimoose
@Guy millimoose 是正确的,你需要强制推送。当然,如果你正在与其他用户共享仓库,或者没有至少与他们协调更改,那么这样做是不推荐的。但如果它是一个私有仓库,那么就没问题了。通过执行 git push origin master -f 来强制推送。此外,在 GitHub 上删除分支默认情况下是不被禁止的,除非你没有远程仓库的写入权限,或者你要求 GitHub 支持禁用删除...你为什么在删除方面遇到麻烦? - user456814
嘿,它成功了!谢谢。在 GitHub 上提交了一个版本。太好了。我所有的 API 密码都在公共仓库里 - 现在没问题了 :-). - Guy

7
如果您不需要保留存储库的配置,最简单的方法就是执行以下操作:
rm -rf .git/
git init .

即使如此,我认为只需要备份~/.git/config~/.git/info/exclude即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接