用Git更改项目的第一次提交?

用Git更改项目的第一次提交?

我希望在我的项目的第一次提交中更改某些内容而不会丢失所有后续提交。有没有办法做到这一点?

我不小心在源代码中的评论中列出了我的原始电子邮件,我想改变它,因为我从机器人索引GitHub收到垃圾邮件。


慕姐4208626
浏览 768回答 3
3回答

qq_笑_17

正如提到的ecdpalma&nbsp;下面,GIT中1.7.12+(2012年8月)具有增强的选项--root为git rebase:“&nbsp;git rebase [-i] --root $tip”现在可用于重写所有导致“&nbsp;$tip”到根提交的历史记录。这个新行为最初在这里讨论:我个人认为“&nbsp;git rebase -i --root”应该只是工作而不需要“&nbsp;--onto”让你“编辑”甚至是历史上的第一个。可以理解的是,没有人会感到困扰,因为人们在历史的最初阶段重写的次数要少得多。随后是补丁。(原答案,2010年2月)正如Git FAQ(和这个SO问题)中提到的,这个想法是:创建新的临时分支将其回滚到您要使用的更改提交&nbsp;git reset --hard更改提交(它将是当前HEAD的顶部,您可以修改任何文件的内容)Rebase分支在更改提交之上,使用:git&nbsp;rebase&nbsp;--onto&nbsp;<tmp&nbsp;branch>&nbsp;<commit&nbsp;after&nbsp;changed>&nbsp;<branch>`诀窍是确保您要删除的信息不会被文件中其他位置的后续提交重新引入。如果您怀疑,那么您必须使用filter-branch --tree-filter以确保该文件的内容在任何提交中都不包含敏感信息。在这两种情况下,您最终都会重写每次提交的SHA1,因此如果您已经发布了要修改其内容的分支,请务必小心。你可能不应该这样做,除非你的项目尚未公开,而其他人没有基于你即将重写的提交工作。

函数式编程

如1.7.12发行说明中所述,您可以使用$&nbsp;git&nbsp;rebase&nbsp;-i&nbsp;--root

阿波罗的战车

git rebase -i允许您方便地编辑除根提交之外的任何先前提交。以下命令显示如何手动执行此操作。# tag the old root, "git rev-list ..." will return the hash of first commitgit tag root `git rev-list HEAD | tail -1`# switch to a new branch pointing at the first commitgit checkout -b new-root root# make any edits and then commit them with:git commit --amend# check out the previous branch (i.e. master)git checkout @{-1}# replace old root with amended versiongit rebase --onto new-root root# you might encounter merge conflicts, fix any conflicts and continue with:# git rebase --continue# delete the branch "new-root"git branch -d new-root# delete the tag "root"git tag -d root
打开App,查看更多内容
随时随地看视频慕课网APP