使用 Git 更改项目的第一次提交? [英] Change first commit of project with Git?

查看:35
本文介绍了使用 Git 更改项目的第一次提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

正如 ecdpalma 所提到的 下面git 1.7.12+(2012 年 8 月)为 git rebase 增强了选项 --root:

"git rebase [-i] --root $tip" 现在可用于重写所有导致 "$tip" 的历史记录到根提交.

最初在此处讨论了这种新行为:

<块引用>

我个人认为git rebase -i --root"应该在不需要--onto"的情况下工作,让你编辑"甚至历史上第一个.
没有人打扰是可以理解的,因为人们在接近历史开始时重写的频率要低得多.

补丁.><小时>

(原始答案,2010 年 2 月)

Git 常见问题 中所述(以及这个 SO 问题),思路是:

  1. 创建新的临时分支
  2. 使用 git reset --hard
  3. 将其回滚到您要更改的提交
  4. 更改提交(它将位于当前 HEAD 的顶部,您可以修改任何文件的内容)
  5. 在更改的提交之上重新建立分支,使用:

    git rebase --onto <更改后提交><分支>`

诀窍是确保您要删除的信息不会被稍后提交的文件中的其他地方重新引入.如果您怀疑,那么您必须使用 filter-branch --tree-filter 确保该文件的内容不包含在任何提交中的合理信息.

在这两种情况下,您最终都会重写每个提交的 SHA1,因此如果您已经发布了要修改其内容的分支,请务必小心.你可能不应该这样做,除非你的项目还没有公开并且其他人没有基于你将要重写的提交工作.

I want to change something in the first commit of my project with out losing all subsequent commits. Is there any way to do this?

I accidentally listed my raw email in a comment within the source code, and I'd like to change it as I'm getting spammed from bots indexing GitHub.

解决方案

As mentioned by ecdpalma below, git 1.7.12+ (August 2012) has enhanced the option --root for git rebase:

"git rebase [-i] --root $tip" can now be used to rewrite all the history leading to "$tip" down to the root commit.

That new behavior was initially discussed here:

I personally think "git rebase -i --root" should be made to just work without requiring "--onto" and let you "edit" even the first one in the history.
It is understandable that nobody bothered, as people are a lot less often rewriting near the very beginning of the history than otherwise.

The patch followed.


(original answer, February 2010)

As mentioned in the Git FAQ (and this SO question), the idea is:

  1. Create new temporary branch
  2. Rewind it to the commit you want to change using git reset --hard
  3. Change that commit (it would be top of current HEAD, and you can modify the content of any file)
  4. Rebase branch on top of changed commit, using:

    git rebase --onto <tmp branch> <commit after changed> <branch>`
    

The trick is to be sure the information you want to remove is not reintroduced by a later commit somewhere else in your file. If you suspect that, then you have to use filter-branch --tree-filter to make sure the content of that file does not contain in any commit the sensible information.

In both cases, you end up rewriting the SHA1 of every commit, so be careful if you have already published the branch you are modifying the contents of. You probably shouldn’t do it unless your project isn’t yet public and other people haven’t based work off the commits you’re about to rewrite.

这篇关于使用 Git 更改项目的第一次提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆