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

后端 未结 30 3149
野性不改
野性不改 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 04:59

    This is a more elaborated version of @Brian's version:

    To change the author and committer, you can do this (with linebreaks in the string which is possible in bash):

    git filter-branch --env-filter '
        if [ "$GIT_COMMITTER_NAME" = "" ];
        then
            GIT_COMMITTER_NAME="";
            GIT_COMMITTER_EMAIL="";
            GIT_AUTHOR_NAME="";
            GIT_AUTHOR_EMAIL="";
        fi' -- --all
    

    You might get one of these errors:

    1. The temporary directory exists already
    2. Refs starting with refs/original exists already
      (this means another filter-branch has been run previously on the repository and the then original branch reference is backed up at refs/original)

    If you want to force the run in spite of these errors, add the --force flag:

    git filter-branch --force --env-filter '
        if [ "$GIT_COMMITTER_NAME" = "" ];
        then
            GIT_COMMITTER_NAME="";
            GIT_COMMITTER_EMAIL="";
            GIT_AUTHOR_NAME="";
            GIT_AUTHOR_EMAIL="";
        fi' -- --all
    

    A little explanation of the -- --all option might be needed: It makes the filter-branch work on all revisions on all refs (which includes all branches). This means, for example, that tags are also rewritten and is visible on the rewritten branches.

    A common "mistake" is to use HEAD instead, which means filtering all revisions on just the current branch. And then no tags (or other refs) would exist in the rewritten branch.

提交回复
热议问题