Several months ago I commited some code in my GitHub repository, but I did it from a temporary system. If I registered my authentication keys correctly to commit stuff, I forgot to create a minimal
~/.gitconfig file with the right stuff in it.
The result was not good looking, as my usual name and mail address were not attached to the commit:
Let’s fix this!
First, get a local copy of the remote Git repository:
$ git clone email@example.com:kdeldycke/kev-code.git
What was missing in my
~/.gitconfig file were the following options:
[user] name = Kevin Deldycke email = firstname.lastname@example.org
These values can be set with Git command line with the following syntax:
--author 'user.name <user.email>'
The commit I want to change is the latest in history, so I’ll use the
--amend directive to make my changes. Putting all things together, our final command becomes:
$ git commit --amend --author 'Kevin Deldycke <email@example.com>'
After this, here is how the local branches looks like in gitg:
git log -n1 command, we can compare the old commit:
commit 81a26f03901918ed4a954d964b2659187f1cc988 Author: kevin <kevin@laptop-kev.(none)> Date: Mon Mar 8 22:49:43 2010 +0100 Update old shop logo with the brand new one
with the new one:
commit adf4620f3d8a89746dd643dcefc3f900f0f69878 Author: Kevin Deldycke <firstname.lastname@example.org> Date: Mon Mar 8 22:49:43 2010 +0100 Update old shop logo with the brand new one
Notice the fixed authorship. The commit ID was also updated as it’s just a hash depending on commit metadata.
Now we can push our changes back to the remote repository:
$ git push origin
But this doesn’t work and throw the following error:
To email@example.com:kdeldycke/kev-code.git ! [rejected] master -> master (non-fast forward) error: failed to push some refs to 'firstname.lastname@example.org:kdeldycke/kev-code.git'
This is Git protection mechanism in action. Modifying already-published commits like this is a bad idea. It can break updates of other developers’ repository (if they already have pulled the commit we’re trying to change).
In our case we will force the remote repository to take our changes:
$ git push origin +master:master
As I told you before this is bad, but nobody really cares: I’m the only person working on this repository! ;)
Finally, you can contemplate the result on GitHub, a clean and tidy commit history: