用Git更改項目的第一次提交? [重複]

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

This question already has an answer here: 這個問題在這裏已有答案:

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分支在更改提交之上,使用:

     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. 如果您懷疑,那麼您必須使用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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章