GIT 修改 COMMIT 的用戶名與郵箱

對於 git 的用戶名與郵箱,咱們大多數人都有兩套,一套公司帳戶平時工做用和一套我的帳戶用於業餘項目。而我又經常在 commit 之後才發現用戶名與郵箱設置錯了。本文總結了如何修改 Commit 的用戶名與郵箱。前端

如何設置用戶名與郵箱

$ git config user.name "Author Name"
$ git config user.email email@address.com
複製代碼

上面的命令只會修改當前項目的配置。若是須要也能夠增長 --global 選項來修改當前系統用戶的默認配置。git

修改 Commit 的用戶名與郵箱

注意: 只建議修改未 push 的 commit。github

由於修改 Commit 的用戶名或郵箱會生成一個新的 commit 來替換以前的 commit 。若是在修改以前已經 push 到了遠端,修改後再次 push 會出現衝突。 只能使用 push -f。 若是其餘人已經拉取( pull )了舊 commit 會出現不少麻煩。spa

只修改最新的 commit

若是你只須要修改最新的 commit ,直接使用:code

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

若是你已經修改了 git config 中的用戶名和郵箱,也可使用cdn

git commit --amend --reset-author --no-edit
複製代碼

若是要修改連續多個 commit

好比,你的 commit 歷史爲 A-B-C-D-E-F , F 爲 HEAD , 你打算修改 C 和 D 的用戶名或郵箱,你須要:blog

  1. 運行 git rebase -i B這裏有一個運行該命令後的例子(英文)
    • 若是你須要修改 A ,能夠運行 git rebase -i --root
  2. 把 C 和 D 兩個 commit 的那一行的 pick 改成 edit
  3. 當 rebase 開始後,將會暫停在 commit C
  4. 運行 git commit --amend --author="Author Name <email@address.com>"
  5. 而後運行 git rebase --continue
  6. 將會繼續暫停在 commit D
  7. 再次運行 git commit --amend --author="Author Name <email@address.com>"
  8. git rebase --continue
  9. rebase 結束
  10. 若是須要更新到遠程倉庫, 使用 git push -f(請確保修改的commit 不會影響其餘人)

參考文章

Stackoverflow: How to change the commit author for one specific commit?ci

原文連接

blog.sjfkai.com/...開發


歡迎關注公衆號 「大前端開發者」。給你帶來更多的前端技術與資訊get

相關文章
相關標籤/搜索