Git 修改 commit 的做者信息

問題

在公司和宿舍使用不一樣的 git 郵箱帳號,結果 commit 的時候忘記修改配置致使 commit 的我的名稱和郵箱都不對git

解決方法

修改 git 配置

git config --global user.email "youremail@googl.com"
git config --global user.name "your name"

可是補救措施只對之後的 commit 起效, 若是想修改以前的做者信息,Github 給出了官方指南:Changing author infogithub

修復 git 歷史提交信息

To change the name and/or email address recorded in existing commits, you must rewrite the entire history of your Git repository.bash

爲了修改 commit 的做者郵箱地址,你必須重寫整個 git 倉庫歷史ide

Warning: This action is destructive to your repository's history. If you're collaborating on a repository with others, it's considered bad practice to rewrite published history. You should only do this in an emergency.this

警告: 這個操做會破壞你的倉庫歷史, 若是你和別人在協同開發這個倉庫,重寫已發佈的歷史記錄是一個很差的操做。建議只在緊急狀況操做code

操做步驟:

  • 打開 bashorm

  • Create a fresh, bare clone of your repository: (新建一個全新的倉庫信息:)ip

git clone --bare https://github.com/user/repo.git
    cd repo.git
  • Copy and paste the script, replacing the following variables based on the information you gathered: (在終端複製並粘貼如下腳本,並將如下的變量修改成你須要的)ci

    **OLD_EMAIL**
    **CORRECT_NAME**
    **CORRECT_EMAIL**
    腳本信息:
    #!/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
  • Press Enter to run the script.(按下 enter 鍵來運行這個腳本開發

  • Review the new Git history for errors.(校對新的 git 倉庫歷史)

  • Push the corrected history to GitHub:(將修改後的倉庫歷史推到遠程)

    git push --force --tags origin 'refs/heads/*'
  • Clean up the temporary clone: (刪除這個倉庫)

    cd ..
    rm -rf repo.git
相關文章
相關標籤/搜索