这个问题在这里已经有了答案:Edit the root commit in Git? (5 个回答) 9 年前关闭。
我想在项目的第一次提交中更改某些内容,而不会丢失所有后续提交。有没有办法做到这一点?
我不小心在源代码的评论中列出了我的原始电子邮件,我想更改它,因为我收到了来自索引 GitHub 的机器人的垃圾邮件。
正如 ecdpalma below 所述,git 1.7.12+(2012 年 8 月)增强了 git rebase
的选项 --root
:
“git rebase [-i] --root $tip
”现在可用于将导致“$tip
”的所有历史记录重写为根提交。
该新行为最初是 discussed here:
我个人认为应该让“git rebase -i --root”不需要“--onto”就可以工作,甚至可以让你“编辑”历史上的第一个。没有人打扰是可以理解的,因为人们在历史开始时重写的频率要低得多。
(原始答案,2010 年 2 月)
如 Git FAQ(和此 SO question)中所述,想法是:
创建新的临时分支 使用 git reset --hard 将其退回到您要更改的提交 更改该提交(它将是当前 HEAD 的顶部,您可以修改任何文件的内容) 在更改的提交之上重新设置分支,使用: git rebase --onto
诀窍是确保您要删除的信息不会被文件中其他地方的稍后提交重新引入。如果您怀疑,那么您必须使用 filter-branch --tree-filter
来确保该文件的内容不包含任何提交中的敏感信息。
在这两种情况下,您最终都会重写每个提交的 SHA1,因此如果您已经发布了要修改其内容的分支,请小心。您可能不应该这样做,除非您的项目尚未公开并且其他人尚未根据您将要重写的提交进行工作。
git rebase -i
允许您方便地编辑任何以前的提交,根提交除外。以下命令向您展示如何手动执行此操作。
# tag the old root, "git rev-list ..." will return the hash of first commit
git tag root `git rev-list HEAD | tail -1`
# switch to a new branch pointing at the first commit
git 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 version
git 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
-a
添加到 git commit --amend
或使用 git add
,因为我第一次忘记了!
如果您只想修改第一次提交,您可以尝试 git rebase 并修改提交,这与这篇文章类似:How to modify a specified commit in git?
如果你想修改所有包含原始电子邮件的提交,filter-branch 是最好的选择。 Pro Git 一书中有一个如何全局更改电子邮件地址的示例,您可能会发现此链接很有用http://git-scm.com/book/en/Git-Tools-Rewriting-History
不定期副业成功案例分享
<commit after changed>
设置为我在git reset --hard
命令中使用的相同哈希值。除了一个小的更改之外,这可以很好地更新存储库中所有提交的作者信息。git rebase -i --root
为我工作。$tip
可以是您想要的任何提交。master
(意思是master HEAD
提交)很好。git reser --soft HEAD~1
来做到这一点,但这是不可能的,因为它是一个模棱两可的参数HEAD~1
或不在工作树中的路径。