Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change author name on git branch

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

  • Change the author and committer name and e-mail of multiple commits in Git
  • Change commit author at one specific commit

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.

like image 858
GantengX Avatar asked Dec 15 '22 11:12

GantengX


2 Answers

git checkout gh-pages
git reset --hard 02bd
git commit --amend --author 'Ronald Suwandi <[email protected]>'
git cherry-pick 02bd..origin/gh-pages
git push -f
like image 75
Zombo Avatar answered Dec 28 '22 19:12

Zombo


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.

like image 45
michas Avatar answered Dec 28 '22 17:12

michas