How to change the commit author for one specific commit?

后端 未结 19 1752
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 05:15

I want to change the author of one specific commit in the history. It\'s not the last commit.

I know about this question - How do I change the author of a commit in

相关标签:
19条回答
  • 2020-11-22 05:37

    The accepted answer to this question is a wonderfully clever use of interactive rebase, but it unfortunately exhibits conflicts if the commit we are trying to change the author of used to be on a branch which was subsequently merged in. More generally, it does not work when handling messy histories.

    Since I am apprehensive about running scripts which depend on setting and unsetting environment variables to rewrite git history, I am writing a new answer based on this post which is similar to this answer but is more complete.

    The following is tested and working, unlike the linked answer. Assume for clarity of exposition that 03f482d6 is the commit whose author we are trying to replace, and 42627abe is the commit with the new author.

    1. Checkout the commit we are trying to modify.

      git checkout 03f482d6
      
    2. Make the author change.

      git commit --amend --author "New Author Name <New Author Email>"
      

      Now we have a new commit with hash assumed to be 42627abe.

    3. Checkout the original branch.

    4. Replace the old commit with the new one locally.

      git replace 03f482d6 42627abe
      
    5. Rewrite all future commits based on the replacement.

      git filter-branch -- --all
      
    6. Remove the replacement for cleanliness.

      git replace -d 03f482d6
      
    7. Push the new history (only use --force if the below fails, and only after sanity checking with git log and/or git diff).

      git push --force-with-lease
      

    Instead of 4-6 you can just rebase onto new commit:

    git rebase -i 42627abe
    
    0 讨论(0)
  • 2020-11-22 05:38

    Github documentation contains a script that replaces the committer info for all commits in a branch.

    • Run the following script from terminal after changing the variable values

      #!/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
      
    • Push the corrected history to GitHub:

      git push --force --tags origin 'refs/heads/*'
      

      OR if you like to push selected references of the branches then use

      git push --force --tags origin 'refs/heads/develop'
      
    0 讨论(0)
  • 2020-11-22 05:38

    Commit before:

    To fix author for all commits you can apply command from @Amber's answer:

    git commit --amend --author="Author Name <email@address.com>"
    

    Or to reuse your name and email you can just write:

    git commit --amend --author=Eugen
    

    Commit after the command:

    For example to change all starting from 4025621:

    You must run:

    git rebase --onto 4025621 --exec "git commit --amend --author=Eugen" 4025621
    

    Note: To include an author containing spaces such as a name and email address, the author must be surrounded by escaped quotes. For example:

    git rebase --onto 4025621 --exec "git commit --amend --author=\"Foo Bar <foo@bar.com>\"" 4025621
    

    or add this alias into ~/.gitconfig:

    [alias]
        reauthor = !bash -c 'git rebase --onto $1 --exec \"git commit --amend --author=$2\" $1' --
    

    And then run:

    git reauthor 4025621 Eugen
    
    0 讨论(0)
  • 2020-11-22 05:40

    you can use these commands from official page of github

    https://help.github.com/en/github/using-git/changing-author-info

    here is the commands

    #!/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
    

    here u can change the old email to ur new user name and email address.

    0 讨论(0)
  • 2020-11-22 05:41

    If the commit that you want to change is not the last commit, then follow the below steps. If your commit is in different branch then first switch to that branch.

    git checkout branch_name

    Find commit before the commit that you want to change and find its hash. Then issue rebase command.

    git rebase -i -p hash of commit

    Then an editor will open and enter 'edit' for the commits that you want to change. Leave others with default 'pick' option. Once changed enter 'esc' key and wq! to exit.

    Then issue git commit command with amendment option.

    git commit --amend --author="Username email" --no-edit

    Then issue the following command.

    git rebase --continue

    Once commit author is updated in the local repository, push the changes to the remote repository.

    0 讨论(0)
  • 2020-11-22 05:42

    There is one additional step to Amber's answer if you're using a centralized repository:

    git push -f to force the update of the central repository.

    Be careful that there are not a lot of people working on the same branch because it can ruin consistency.

    0 讨论(0)
提交回复
热议问题