How to change multiple commits in Git to change author

I made a series of commits in Git, and now I understand that I forgot to configure the properties of the username and email user (new machine) correctly. I haven't pushed these commits to my repository yet, so how can I fix these commits before I do this (only the last 3 commits on the main branch)?

I watched git reset and git commit -C <id> --reset-author , but I don't think I'm on the right track.

+136
git git-commit git-rewrite-history
Feb 12 '11 at 22:48
source share
6 answers

Rebase / modify seems inefficient when you have the power of the filter branch:

 git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then GIT_AUTHOR_EMAIL=correct@email; GIT_AUTHOR_NAME="Correct Name"; GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL; GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all 

(split lines for clarity, but not required)

Be sure to check the result when you are done to make sure that you haven’t changed anything, that you didn’t want to!

+176
Feb 13 2018-11-11T00:
source share

The interactive redirection approach is pretty good when used in conjunction with exec. You can run any shell command against a specific commit or all commits in rebase.

Set git author settings first

 git config --global user.name "John Doe" git config --global user.email johndoe@example.com 

Then, until reset, the author commits for everyone after the specified SHA

 git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD" 

This will bring up your editor to confirm the changes. All you have to do is save and exit, and it will go through each commit and run the command indicated in the -x flags.

In the @Dave comment below, you can also change the author while preserving the original timestamps with:

 git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <new_address@example.com>' -CHEAD" 
+116
Sep 12 '14 at 19:05
source share

To change the author only for the last commit:

 git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit 

Suppose you want to change the author only for the last N commits:

 git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit" 

NOTES

  • the --no-edit flag ensures that git commit --amend does not ask for additional confirmation
  • when you use git rebase -i , you can manually select commits where you can change the author,

the file you are editing will look like this:

 pick 897fe9e simplify code a little pick abb60f9 add new feature exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit pick dc18f70 bugfix 
+58
Jul 28 '15 at 7:19
source share

I believe you are looking for git rebase --interactive

This allows you to reset to a specific commit, and then move on to changing the history of adding or grouping commits.

Here you have an explanation https://web.archive.org/web/20100213104931/http://blog.madism.org/index.php/2007/09/09/138-git-awsome-ness-git-rebase -interactive

+3
Feb 12 '11 at 23:16
source share

If you feel unsafe with regards to reduction and correction, you can do it this way. At the same time, you'll also be setting up a global configuration, which you probably would like to do anyway.

git reset HEAD~ (discard last commit)

git config --global user.name "Your Name"

git config --global user.email you@example.com

git commit -m "message"

0
13 Oct '17 at 3:21 on
source share

This method has been documented by github for this purpose. Steps:

  1. Open a terminal and make a naked clone of your repo
 git clone --bare https://github.com/user/repo.git cd repo 
  1. Edit the following script (replacing OLD_EMAIL , CORRECT_EMAIL and CORRECT_NAME )
 #!/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 
  1. Copy / paste the script into your terminal and press Enter to start it.
  2. git push --force --tags origin 'refs/heads/*' your changes with git push --force --tags origin 'refs/heads/*' and git push --force --tags origin 'refs/heads/*' done!
0
Feb 12 '19 at 6:42
source share



All Articles