Having issues with your git history? Perhaps you're not finding that you're credited with commits you believe that you should. This can be caused by a number of issues --perhaps your email is wrong on github, or incorrectly set in your local .git file. There are actually loads of reasons why this might be messed up, and while you should fix that --this guide is only going to teach you how to rewrite this evil, not how to prevent it.
The first thing we should do is learn a couple of helpful commands that will show us what needs to be fix and indicate our progress.
These must be performed inside of the repository about which you wish to glean information.
git shortlog -sn
This command will show the number of commits and author name sorted from most to least commits as shown below:
git log --pretty="%an %ae%n%cn %ce | sort | uniq
This command will show all of the known name and email tuples from commits and sort them alphabetically. This way, you can see which emails need to be replaced. An example is below:
- Clone the git repo you intend to rewrite
- cd into that repo
- Download this shell script
- Open that shell script and replace what
OLD_EMAIL
,CORRECT_NAME
, andCORRECT_EMAIL
are equal to so that the script will accomplish the change you desire. Refer to the earlier helpful commands if you are unaware of the old email. - Save your changes and commit the new shell script into your repository; funny enough, you will be unable to run it if there are unstaged or uncommited changes
- Run the shell script with the command
sh name.sh
where name is the name of the script - Wait for that to finish
- To make those changes official, use the command
git push --force --tags origin 'refs/heads/*'
(By the way, these changes won't apply to protected branches) - Wait again
- To remove the old history, use the command
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch Rakefile' HEAD
- Wait again again
- Now you're done! Repeat steps 4 through 11 as necessary to change all the history you desire and then...
- Delete the shell script from your repo and commit that delete
When you're done, you should see a change in the output of the helpful commands. For example, the first of the helpful commands will show a change in the number of commits attributed to the user you are not applying them to. See the image below and compare it to the one above, paying special attention to the user Zachary Danz: