0

If I have 200 commits that I have pushed with wrong author information in git config --global user.email and git config --global user.name. How can I change these commits afterwards in a command without having to go through every one of each?

All commits are of the same author and some commits are between with a different author. So is it possible to iterate over each commit FROM and TO commits and send a git push --force-with-lease command to update all of these in the history of the master branch?

Any idea?

1 Answers1

0

If they are in a straight line, you could pull it off with a little bit of bash-fu:

git checkout first-commit-to-adjust~ # careful with the pig-tail... it has to be there
git log --pretty=%h --reverse HEAD..last-one-to-adjust | while read commit; do
    git cherry-pick $commit
    # here we need a check for the author name
    if [ "$( git show --summary --pretty=%an --quiet )" == "misnamed fulanito de tal" ]; then
        # yep, it's me
        git commit --amend --no-edit --reset-author
    fi
done

If you want to only set author name/email (not modifying author date), I think you will have to set values for GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables and then issue the git commit --amend --no-edit command. Concept does not change.... and if you want to control more things, then there are more environment variables at your disposal to play with. Check git help commit.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • `--reset-author` also sets the current timestamp as the author date – axiac Nov 04 '22 at 10:18
  • 1
    Then it's just about playing with environment variables.... concept is the same. – eftshift0 Nov 04 '22 at 10:18
  • Thanks! Just to clarify, `last-one-to-adjust` should it expect a number? –  Nov 04 '22 at 10:30
  • it's the ID of the first commit you want to adjust. – eftshift0 Nov 04 '22 at 10:33
  • @eftshift0 It worked fine, just one thing now. How can I push these changes to origin of the master branch. Now it's currently in a detached mode. –  Nov 04 '22 at 11:16
  • 1
    You can set the local branch like this: `git branch -f the-local-branch; git checkout the-local-branch`. Then you can force-push it where you need it – eftshift0 Nov 04 '22 at 11:18
  • Thanks a lot for this! You got your answer as the accepted one. –  Nov 04 '22 at 11:20