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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2246208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:50:34  来源:igfitidea点击:

Change first commit of project with Git?

gitrebasegit-filter-branch

提问by Michael

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 索引机器人的垃圾邮件。

回答by VonC

As mentioned by ecdpalmabelow, git 1.7.12+(August 2012) has enhanced the option --rootfor git rebase:

正如提到的ecdpalma下面GIT中1.7.12+(2012年8月)具有增强的选项--rootgit rebase

"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.
It is understandable that nobody bothered, as people are a lot less often rewriting near the very beginning of the history than otherwise.

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

The patch followed.

补丁其次



(original answer, February 2010)

(原始答案,2010 年 2 月)

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

正如Git 常见问题解答(以及这个SO question)中提到的,这个想法是:

  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>`
    
  1. 创建新的临时分支
  2. 将其倒回到您要使用的提交 git reset --hard
  3. 更改该提交(它将位于当前 HEAD 的顶部,您可以修改任何文件的内容)
  4. 在更改的提交之上重新设置分支,使用:

    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-filterto 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. 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.

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

回答by ecdpalma

As stated in 1.7.12 Release Notes, you may use

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

$ git rebase -i --root

回答by cmcginty

git rebase -iallows you to conveniently edit any previous commits, except for the root commit. The following commands show you how to do this manually.

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

回答by ZelluX

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. 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

如果你想修改所有包含原始电子邮件的提交,filter-branch 是最好的选择。在Pro Git一书中有一个关于如何全局更改电子邮件地址的示例,您可能会发现此链接很有用http://git-scm.com/book/en/Git-Tools-Rewriting-History