天天看点

用Git更改项目的第一次提交? [重复]

本文翻译自:Change first commit of project with Git? [duplicate]

This question already has an answer here:

这个问题在这里已有答案:
  • Edit the root commit in Git? 在Git中编辑root提交? 5 answers 5个答案

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.

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

#1楼

参考:https://stackoom.com/question/9QLA/用Git更改项目的第一次提交-重复

#2楼

如1.7.12发行说明中所述 ,您可以使用

$ git rebase -i --root
           

#3楼

If you want to modify only the first commit, you may try git rebase and amend the commit, which is similar to this post: How to modify a specified commit in git?

如果你只想修改第一个提交,你可以尝试git rebase并修改提交,这与这篇文章类似: 如何在git中修改指定的提交?

And if you want to modify all the commits which contain the raw email, filter-branch is the best choice.

如果要修改包含原始电子邮件的所有提交,filter-branch是最佳选择。

There is an example of how to change email address globally on the book Pro Git , and you may find this link useful http://git-scm.com/book/en/Git-Tools-Rewriting-History

有一个如何在Pro Git书上全局更改电子邮件地址的示例,您可能会发现此链接很有用http://git-scm.com/book/en/Git-Tools-Rewriting-History

#4楼

As mentioned by ecdpalma below , git 1.7.12+ (August 2012) has enhanced the option

--root

for

git rebase

:

如下面的ecdpalma 所述 , git 1.7.12 + (2012年8月)增强了

git rebase

的选项

--root

"

git rebase [-i] --root $tip

" can now be used to rewrite all the history leading to "

$tip

" down to the root commit.

git rebase [-i] --root $tip

”现在可以用来重写导致“

$tip

”到根提交的所有历史记录。

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. 我个人认为“

git rebase -i --root

”应该只需要工作而不需要“

--onto

”,让你“编辑”甚至是历史上的第一个。
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)

(原答案,2010年2月)

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

正如Git FAQ (和这个SO问题 )中提到的,这个想法是:
  1. Create new temporary branch 创建新的临时分支
  2. Rewind it to the commit you want to change using

    git reset --hard

    使用

    git reset --hard

    将其回滚到要更改的提交
  3. Change that commit (it would be top of current HEAD, and you can modify the content of any file) 更改提交(它将是当前HEAD的顶部,您可以修改任何文件的内容)
  4. Rebase branch on top of changed commit, using: Rebase分支在更改提交之上,使用:

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.

如果您怀疑,那么您必须使用

filter-branch --tree-filter

来确保该文件的内容在任何提交中都不包含敏感信息。

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.

在这两种情况下,您最终都会重写每次提交的SHA1,因此如果您已经发布了要修改其内容的分支,请务必小心。

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.

你可能不应该这样做,除非你的项目尚未公开,而其他人没有基于你即将重写的提交工作。

#5楼

git rebase -i

allows you to conveniently edit any previous commits, except for the root commit .

git rebase -i

允许您方便地编辑除根提交之外的任何先前提交 。

The following commands show you how to do this manually.

以下命令显示如何手动执行此操作。
# 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