How to change the commit author for one specific commit?

后端 未结 19 1854
没有蜡笔的小新
没有蜡笔的小新 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:54

    Interactive rebase off of a point earlier in the history than the commit you need to modify (git rebase -i <earliercommit>). In the list of commits being rebased, change the text from pick to edit next to the hash of the one you want to modify. Then when git prompts you to change the commit, use this:

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

    For example, if your commit history is A-B-C-D-E-F with F as HEAD, and you want to change the author of C and D, then you would...

    1. Specify git rebase -i B (here is an example of what you will see after executing the git rebase -i B command)
      • if you need to edit A, use git rebase -i --root
    2. Change the lines for both C and D from pick to edit
    3. Exit the editor (for vim, this would be pressing Esc and then typing :wq).
    4. Once the rebase started, it would first pause at C
    5. You would git commit --amend --author="Author Name <email@address.com>"
    6. Then git rebase --continue
    7. It would pause again at D
    8. Then you would git commit --amend --author="Author Name <email@address.com>" again
    9. git rebase --continue
    10. The rebase would complete.
    11. Use git push -f to update your origin with the updated commits.
    0 讨论(0)
  • 2020-11-22 05:55

    The answers in the question to which you linked are good answers and cover your situation (the other question is more general since it involves rewriting multiple commits).

    As an excuse to try out git filter-branch, I wrote a script to rewrite the Author Name and/or Author Email for a given commit:

    #!/bin/sh
    
    #
    # Change the author name and/or email of a single commit.
    #
    # change-author [-f] commit-to-change [branch-to-rewrite [new-name [new-email]]]
    #
    #     If -f is supplied it is passed to "git filter-branch".
    #
    #     If <branch-to-rewrite> is not provided or is empty HEAD will be used.
    #     Use "--all" or a space separated list (e.g. "master next") to rewrite
    #     multiple branches.
    #
    #     If <new-name> (or <new-email>) is not provided or is empty, the normal
    #     user.name (user.email) Git configuration value will be used.
    #
    
    force=''
    if test "x$1" = "x-f"; then
        force='-f'
        shift
    fi
    
    die() {
        printf '%s\n' "$@"
        exit 128
    }
    targ="$(git rev-parse --verify "$1" 2>/dev/null)" || die "$1 is not a commit"
    br="${2:-HEAD}"
    
    TARG_COMMIT="$targ"
    TARG_NAME="${3-}"
    TARG_EMAIL="${4-}"
    export TARG_COMMIT TARG_NAME TARG_EMAIL
    
    filt='
    
        if test "$GIT_COMMIT" = "$TARG_COMMIT"; then
            if test -n "$TARG_EMAIL"; then
                GIT_AUTHOR_EMAIL="$TARG_EMAIL"
                export GIT_AUTHOR_EMAIL
            else
                unset GIT_AUTHOR_EMAIL
            fi
            if test -n "$TARG_NAME"; then
                GIT_AUTHOR_NAME="$TARG_NAME"
                export GIT_AUTHOR_NAME
            else
                unset GIT_AUTHOR_NAME
            fi
        fi
    
    '
    
    git filter-branch $force --env-filter "$filt" -- $br
    
    0 讨论(0)
  • 2020-11-22 05:57

    There is also a lazy approach to this problem, especially if you have more than one commit that you want to change. In my case, I had a new branch with several commits with a wrong author, so what helped me:

    Go to your original branch:

    git checkout develop
    

    Create new branch from it:

    git checkout -b myFeature develop 
    

    Merge it without commit info as one commit:

    git merge --no-commit --squash branchWrongAuthor
    

    You might also want to stage changes:

    git stage .
    

    Change the name of the author and commit changes:

    git commit --amend --author "New Author Name <New Author Email>" -m "new feature added"
    

    And that's it, you can push the changes.

    git push
    

    You can delete the branch with a wrong author after that.

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

    In furtherance to Eugen Konkov answer, to start from the root commit, use --root flag. The --no-edit flag is helpful too, because with it you are not prompted into an editor for each commit.

    git rebase --root --exec "git commit --amend --author='name <email>' --no-edit"
    
    0 讨论(0)
  • 2020-11-22 05:59

    Steps to rename author name after commit pushed

    1. First type "git log" to get the commit id and more details
    2. git rebase i HEAD~10 (10 is the total commit to display on rebase)

      If you Get anything like below

      fatal: It seems that there is already a rebase-merge directory, and I wonder if you are in the middle of another rebase. If that is the case, please try

      git rebase (--continue | --abort | --skip) If that is not the case, please rm -fr ".git/rebase-merge" and run me again. I am stopping in case you still have something valuable there.

    3. Then type "git rebase --continue" or "git rebase --abort" as per your need

      • now your will rebase window opened, click "i" key from keyboard
      • then you will get list of commits to 10 [because we have passed 10 commit above] Like below

      pick 897fe9e simplify code a little

      pick abb60f9 add new feature

      pick dc18f70 bugfix

    4. Now you need to add below command just below of the commit you want to edit, like below

      pick 897fe9e simplify code a little exec git commit --amend --author 'Author Name <author.name@mail.com>' pick abb60f9 add new feature exec git commit --amend --author 'Author Name <author.name@mail.com>' pick dc18f70 bugfix exec git commit --amend --author 'Author Name <author.name@mail.com>'

      1. That's it, now just press ESC, :wq and you are all set

      2. Then git push origin HEAD:BRANCH NAME -f [please take care of -f Force push]

      like git push -f or git push origin HEAD: dev -f

    0 讨论(0)
  • 2020-11-22 06:00

    Changing Your Committer Name & Email Globally:

    $ git config --global user.name "John Doe"
    $ git config --global user.email "john@doe.org"
    

    Changing Your Committer Name & Email per Repository:

    $ git config user.name "John Doe"
    $ git config user.email "john@doe.org"
    

    Changing the Author Information Just for the Next Commit:

    $ git commit --author="John Doe <john@doe.org>"
    

    Hint: For other situation and read more information read the post reference.

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