Skip to content

Instantly share code, notes, and snippets.

@patelkunal
Last active August 29, 2015 14:15
Show Gist options
  • Save patelkunal/f1143241e7fb6bfb4fdd to your computer and use it in GitHub Desktop.
Save patelkunal/f1143241e7fb6bfb4fdd to your computer and use it in GitHub Desktop.
How to fix git commit with wrong email address and name - before pushing
If:
- you add and commit with the wrong email address in git, and
- your remote has a hook set up to prevent you from pushing with the bad address
Then you need to amend the author of your commit before push can succeed:
1. fix your email address in git config:
$ git config user.name "kppatel"
$ git config user.email "[email protected]"
2. Find oldest commit which you want to update - pick up its ancestor/parent commit hash
git log --format=full
2. then do a rebase:
$ git rebase -i -p 0cfc972 # this is just a random commit
# option -i is for interactive mode in rebase
# option -p preserves your merges in git-tree - Advisable to use -p which stops you from messing up merge-trees in your repo.
# git brings up your editor.
3. in the editor, mark the commit as 'edit' then save and exit the editor
#before
pick 0cfc972 Someone else commit
pick 0cfc971 my bad commit 1
pick 0cfc970 my bad commit 2
# ..
# ...
#change should look like this
pick 0cfc972 Someone else commit
edit 0cfc971 my bad commit 1
edit 0cfc970 my bad commit 2
# save and quite editor
4. do an amend that resets the author now that your address is changed:
$ git commit --amend --reset-author
5. finish with a rebase:
$ git rebase --continue
# Here in above, 3, 4 and 5 th step will be performed on HEAD instead of master/ your branch
# once everything completes successfully, git changes branch to master/your branch
# git says:
# Successfully rebased and updated refs/heads/master.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment