Question

I branched my github project to create the static page the other day but messed up with my author name

https://github.com/ronaldsuwandi/le-simplepage/commits/gh-pages

How do I rename the author for the first commit for the branch?

I've tried

But no luck - I'm still really new to git..

EDIT: The answer given by Amber does not work if I want to change the first commit (e.g. if my commit history is A-B-C-D and I would like to change the commit history of B I can do git rebase -i A. But I'm not sure what to do if I want to change commit history of A itself.

Was it helpful?

Solution

git checkout gh-pages
git reset --hard 02bd
git commit --amend --author 'Ronald Suwandi <ronald@ronaldsuwandi.com>'
git cherry-pick 02bd..origin/gh-pages
git push -f

OTHER TIPS

Just as in real life, changing history is not a trivial thing. ;)

In git a commit includes (among others) both the author name and the parent commit. Therefore you cannot simply change attributes like the author of a commit. The only thing you can do is throw away the old commit and create a new fixed one. But as the next commit did contain the original commit as a parent you have to change that commit, too. - But again you cannot do this but you need to replace the old one, etc.

There is git rebase to help you with this kind of ugly things: It takes a series of commits and creates a completely new one with the changed history. As a branch in git is simply a pointer to a single commit, the rebase then just changes that pointer from the old history to the new one.

X-A-B-C-D <- "old master"
 `a-b-c-d <- "new master"

As long as you are working on your local repository it is no problem to change history as much as you like. Things change as soon as you publish your commits by pushing them to a shared repository like github. If you already published the old history and now publish the new history, both versions are published and if others work on that repository, too, chances are good that the old version will find its way back to your history.

So generally just do not change history once you have published it. But if you are sure no one else is working on your remote repository you can do it anyways: Fix your history locally and force push it to the repository.

Changing history locally is easy with git rebase -i. Unfortunately it is a bit more tricky for the root commit of your repository. See Change first commit of project with Git? for details.

Git Tower has a very useful page that details a few different methods for changing the author

You can use the following script on commandline to fix incorrect author names in a repo.

$ git filter-branch --env-filter '
WRONG_EMAIL="wrong@example.com"
NEW_NAME="New Name Value"
NEW_EMAIL="correct@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$NEW_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$NEW_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

https://www.git-tower.com/learn/git/faq/change-author-name-email

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top