How to change the author and committer name and e-mail of multiple commits in Git?

后端 未结 30 3130
野性不改
野性不改 2020-11-21 04:50

I was writing a simple script in the school computer, and committing the changes to Git (in a repo that was in my pendrive, cloned from my computer at home). After several c

相关标签:
30条回答
  • 2020-11-21 05:10

    Github has a nice solution, which is the following shell script:

    #!/bin/sh
    
    git filter-branch --env-filter '
    
    an="$GIT_AUTHOR_NAME"
    am="$GIT_AUTHOR_EMAIL"
    cn="$GIT_COMMITTER_NAME"
    cm="$GIT_COMMITTER_EMAIL"
    
    if [ "$GIT_COMMITTER_EMAIL" = "your@email.to.match" ]
    then
        cn="Your New Committer Name"
        cm="Your New Committer Email"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "your@email.to.match" ]
    then
        an="Your New Author Name"
        am="Your New Author Email"
    fi
    
    export GIT_AUTHOR_NAME="$an"
    export GIT_AUTHOR_EMAIL="$am"
    export GIT_COMMITTER_NAME="$cn"
    export GIT_COMMITTER_EMAIL="$cm"
    '
    
    0 讨论(0)
  • 2020-11-21 05:11

    You can use this as a alias so you can do:

    git change-commits GIT_AUTHOR_NAME "old name" "new name"
    

    or for the last 10 commits:

    git change-commits GIT_AUTHOR_EMAIL "old@email.com" "new@email.com" HEAD~10..HEAD
    

    Add to ~/.gitconfig:

    [alias]
        change-commits = "!f() { VAR=$1; OLD=$2; NEW=$3; shift 3; git filter-branch --env-filter \"if [[ \\\"$`echo $VAR`\\\" = '$OLD' ]]; then export $VAR='$NEW'; fi\" $@; }; f "
    

    Source: https://github.com/brauliobo/gitconfig/blob/master/configs/.gitconfig

    Hope it is useful.

    0 讨论(0)
  • 2020-11-21 05:12

    You can also do:

    git filter-branch --commit-filter '
            if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
            then
                    GIT_COMMITTER_NAME="<New Name>";
                    GIT_AUTHOR_NAME="<New Name>";
                    GIT_COMMITTER_EMAIL="<New Email>";
                    GIT_AUTHOR_EMAIL="<New Email>";
                    git commit-tree "$@";
            else
                    git commit-tree "$@";
            fi' HEAD
    

    Note, if you are using this command in the Windows command prompt, then you need to use " instead of ':

    git filter-branch --commit-filter "
            if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
            then
                    GIT_COMMITTER_NAME="<New Name>";
                    GIT_AUTHOR_NAME="<New Name>";
                    GIT_COMMITTER_EMAIL="<New Email>";
                    GIT_AUTHOR_EMAIL="<New Email>";
                    git commit-tree "$@";
            else
                    git commit-tree "$@";
            fi" HEAD
    
    0 讨论(0)
  • 2020-11-21 05:13

    It happens when you do not have a $HOME/.gitconfig initialized. You may fix this as:

    git config --global user.name "you name"
    git config --global user.email you@domain.com
    git commit --amend --reset-author
    

    tested with git version 1.7.5.4

    0 讨论(0)
  • 2020-11-21 05:13

    I found the presented versions way to aggressive, especially if you commit patches from other developers, this will essentially steal their code.

    The version below does work on all branches and changes the author and comitter separately to prevent that.

    Kudos to leif81 for the all option.

    #!/bin/bash
    
    git filter-branch --env-filter '
    if [ "$GIT_AUTHOR_NAME" = "<old author>" ];
    then
        GIT_AUTHOR_NAME="<new author>";
        GIT_AUTHOR_EMAIL="<youmail@somehost.ext>";
    fi
    if [ "$GIT_COMMITTER_NAME" = "<old committer>" ];
    then
        GIT_COMMITTER_NAME="<new commiter>";
        GIT_COMMITTER_EMAIL="<youmail@somehost.ext>";
    fi
    ' -- --all
    
    0 讨论(0)
  • 2020-11-21 05:13

    Note that git stores two different e-mail addresses, one for the committer (the person who committed the change) and another one for the author (the person who wrote the change).

    The committer information isn't displayed in most places, but you can see it with git log -1 --format=%cn,%ce (or use show instead of log to specify a particular commit).

    While changing the author of your last commit is as simple as git commit --amend --author "Author Name <email@example.com>", there is no one-liner or argument to do the same to the committer information.

    The solution is to (temporarily, or not) change your user information, then amend the commit, which will update the committer to your current information:

    git config user.email my_other_email@example.com 
    git commit --amend
    
    0 讨论(0)
提交回复
热议问题