After the last number of weeks I have been working away with some projects / repositories on GitHub. As I’m currently the only contributor to my repos my usage of the full capabilities of git isn’t necessitated and to be frank most of my work with git has simply involved;
vim some-super-interesting-file.md
:wq
git add some-super-interesting-file.md
git commit -m "some commit message"
git push origin whatever-branch
It’s pretty simple and straightforward and outside of needing to do the odd git pull
in my project directory from time to time because I’ve done some updates either directly on github.com or on another machine, it’s been pretty flawless.
However as part of my exploration and learning I’ve been clone/forking the odd repo too and to be honest doing a little blind following of online tutorials. One such tutorial included a capability to run a system wide update to your machine’s git installation. Unbeknownst to me this was also updating the system-wide git config parameters including important items such as site.name
or site.email
or github.user
In the main a file in your user home dir ~/.gitconfig
sets your git configuration system-wide. It’s a wise move therefore to ensure that this is not overwritten or changed without you knowing/understanding. For me I came to this realisation a little late and somewhat by accident. I’d made a number of git pushes a couple of days ago and noticed earlier today that several of the most recent commits had been written by a user other than me. I did some googling as you do and then checked my repo config using git config -l
to display the FULL configuration parameters pertaining to my repo.
After a bit of digging and updating my system git config (~/.gitconfig) in my case, I stumbled upon this really helpful script which allows you to easily search for commits made by a specific site.name or site.user for instance and correct that.
Here’s the steps to take and a little pitfall I found due to the ’ in my name.
- Create a brand new BARE clone of the repo in which you need to retrospectively update the commit authors in;
git clone --bare https://github.com/user/repo.git
cd repo.git
- Copy and paste the script below, replacing the following variables based on the information you gathered;
OLD_EMAIL
CORRECT_NAME
CORRECT_EMAIL
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
- Press ENTER to run the script and update the author
- What I noticed at this point is that the shell script was not running and this was because the ’ in my name i.e. Mick O‘Donovan was closing part of the command earlier. For a quick fix I simply set my CORRECT_NAME variable as “Mick O Donovan” dropping the apostrophe.
-
Review the new Git history for errors (for me thankfully there were none)
-
Push this corrected history to GitHub;
git push --force --tags origin 'refs/heads/*'
- Once done the cloned BARE directory is no longer needed and can be removed with an
rm -rf
of the directory.
Thought I should create a blog post as a reference document to refer back to if I’m silly enough to do this again in the future. (there’s almost certainly a cleaner way to set my name to include the ’ but for now this isn’t needed)