views:

543

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, and I'd like to change it as I'm getting spammed from bots indexing github.)

Edit/Addendum

(whereby "listed my raw email" I mean listed my raw email in a comment in the source — not in a git config.)

+1  A: 

See this at Serverfault: http://serverfault.com/questions/12373/how-do-i-edit-gits-history-to-correct-an-incorrect-email-address-name

Hans W
Sorry — although this is useful, it doesn't answer my question. The email is listed in a text file in the first commit, it's not just the committer's email I need to change.
meeselet
A: 

You are probably already hosed. Once they harvest your email address, they'll make a few extra bucks by selling it to other spammers (who will then make a few extra bucks by selling it to others...). Once it gets out there, they all have it.

T.E.D.
Probably. But I would still like to change it.
meeselet
A: 

If you want to modify only the first commit, you may try git rebase and amend the commit, which is similar to this post: http://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit/

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://progit.org/book/ch6-4.html#changing_email_addresses_globally

ZelluX
+2  A: 

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

  • Create new temporary branch,
  • rewind it to the commit you want to change using git reset --hard,
  • change that commit (it would be top of current head, and you can modify the content of any file),
  • then 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 case, you end up rewriting the SHA1 of every commits, so be careful if you have already published the branch you are modifying the content 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.

VonC
Thanks — that SO question you linked to really cleared it up.
meeselet
A: 

git rebase -i allows you to conveniently edit any previous commits, except for the root commit. 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
Casey