有時候,公司提交的程式碼必須使用公司郵箱,而你誤操作,直接把自己個人郵箱提交上去了,此時你就會遇到這樣的需求:如何修改git已提交的郵箱?
而這個需求對於新手來說,往往要花費半天的時間才能理解修改過程,簡直太傻比了,所以我這裡做一個詳細的文件來幫助自己和你搞清楚這個流程。尤其要理解變基,它不是一個命令執行就完成了,而是一連串命令的組合。
變基
git rebase -i
複製程式碼
執行後,會開啟最近一條的提交記錄,當然上面的命令可以指定某一條記錄,命令是:
git rebase -i "your commit id"
複製程式碼
對於sourcetree使用者來說,commit id是SHA-1,可以右鍵某條提交記錄,選擇選單"複製SHA-1到剪貼簿",如下圖:

變基rebase命令執行完成後,會列印類似如下內容:
pick bd81df5 更新API
# Rebase abcb9d0..bd81df5 onto abcb9d0 (1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
複製程式碼
新手往往會一臉懵逼,不止所錯,此時是在rebase的過程中,你需要把pick改為edit,如下:
edit bd81df5 更新API
# Rebase abcb9d0..bd81df5 onto abcb9d0 (1 command)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
複製程式碼
更改完成後,儲存並退出vi編輯器::wq
然後會列印這樣的訊息:
chengmingdeMacBook-Pro:server cmlanche$ git rebase -i "abcb9d0d1e99cdad25d8d08119e494436b000e59"
Stopped at bd81df5... 更新API
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
chengmingdeMacBook-Pro:server cmlanche$
複製程式碼
給大家先科普一下這個amend
英文單詞,是修改的意思,對我來說好陌生,為啥不用change或者fix之類的。

上面的資訊說了,如果你要amend,也就是要修改這個提交的話,那麼用
git commit --amend
複製程式碼
如果你對這次修改滿意的話,就用如下命令結束此次變基
git rebase --continue
複製程式碼
重置賬戶郵箱資訊
我們當然要修改啦,那麼執行如下命令,重置提交的賬戶資訊:
git commit --amend --author="cmlanche <1204833748@qq.com>" --no-edit
複製程式碼
同事,要注意你的sourcetree,出現了新情況!

我們可以看到一個新的提交,並且,郵箱賬號都經過了修改,如果你去掉--no-edit
還可以修改commit message,也就是圖中的"更新API",舉栗子吧,我可以繼續用amend修改此次變基
git commit --amend --author="cmlanche <1204833748@qq.com>"
複製程式碼

儲存退出vi編輯器,看sourcetree咋樣了:

真的很完美,接下來就是合併了,退出變基。
退出變基
git rebase --continue
複製程式碼
在控制檯中列印如上命令退出變基,我們看到退出變基也就是使用最新的修改了,就一條分支了。
chengmingdeMacBook-Pro:server cmlanche$ git rebase --continue
Successfully rebased and updated refs/heads/bulma.
複製程式碼

最後總結一下
變基真的很有用,他不是一條命令搞定的,是一個過程,就像變成中開啟了一個輸入流,最後用完你得關閉輸入流一樣。
通過變基你可以輕鬆實現提交資訊的任意重新修改!