Setting email in git commit
When you first set up a new dev environment, you’ll want to set the name and email you use for Git commits. The easy way to do this is with two commands:
git config --global user.name "Adam O'Grady" git config --global user.email "email@example.com"
This means all future commits in this dev environment will use that name and email address combination.
In some cases you may want to use different details for a particular repo. In that case you use the same commands without the
--global flag, like so:
git config user.name "Adam O'Grady" git config user.email "firstname.lastname@example.org"
Fixing email in commits
Warning: The following changes do edit the repository history. Do not take them lightly, you are potentially changing old information useful for auditing code.
Fixing last commit
If you realise you just need to adjust the last commit, there’s a handy one-liner for that:
git commit --amend --author="Adam O'Grady <email@example.com>"
See what it does? It changes the last commit, but only amends the author name and email.
Editing multiple commits
If you think you’ve put the wrong email address in a historical commit, thankfully you’re only a few simple commands away from fixing it. Firstly, check the log of your commits to find where you put the wrong details:
This shows a historical log that you can scroll with the up and down arrow keys or jump page at a time with the space bar.
Once you’ve discovered the errant commit(s), find the first good commit before that and copy the SHA1 hash (the string of letters and numbers after the word “commit”) and run the following command:
git rebase -i -p bb1506a
You’ll notice I included only the first 7 characters, this is completely valid or you can use the full hash. This command opens an interactive prompt in the vim text editor. press the “a” key to enter the insert mode find the lines which relate to known-bad commits. Navigate around the document and change each
edit for the errant commits. Once you’re done hit the escape key to go back to the command mode and type “:wq” to save and exit.
To edit the commit:
git commit --amend --author="Adam O'Grady <firstname.lastname@example.org>" --no-edit
This will change the email address on the current commit in the rebase operation. Once that’s done you can move to the next commit with:
git rebase --continue
Repeat the last two steps until all commits have been fixed.