有時(shí)候晾虑,公司提交的代碼必須使用公司郵箱疹味,而你誤操作,直接把自己個(gè)人郵箱提交上去了帜篇,此時(shí)你就會(huì)遇到這樣的需求:如何修改git已提交的郵箱糙捺?
而這個(gè)需求對(duì)于新手來說,往往要花費(fèi)半天的時(shí)間才能理解修改過程笙隙,簡(jiǎn)直太傻比了洪灯,所以我這里做一個(gè)詳細(xì)的文檔來幫助自己和你搞清楚這個(gè)流程。尤其要理解變基竟痰,它不是一個(gè)命令執(zhí)行就完成了签钩,而是一連串命令的組合。
步驟1:變基
git rebase -i
執(zhí)行后坏快,會(huì)打開最近一條的提交記錄铅檩,當(dāng)然上面的命令可以指定某一條記錄,命令是:
git rebase -i "your commit id"
對(duì)于sourcetree用戶來說莽鸿,commit id是SHA-1昧旨,可以右鍵某條提交記錄拾给,選擇菜單"復(fù)制SHA-1到剪貼板",如下圖:
變基rebase命令執(zhí)行完成后兔沃,會(huì)打印類似如下內(nèi)容:
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
新手往往會(huì)一臉懵逼蒋得,不止所錯(cuò),此時(shí)是在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
然后會(huì)打印這樣的消息:
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$
給大家先科普一下這個(gè)amend
英文單詞缰雇,是修改的意思入偷,對(duì)我來說好陌生,為啥不用change或者fix之類的械哟。
上面的信息說了疏之,如果你要amend,也就是要修改這個(gè)提交的話暇咆,那么用
git commit --amend
如果你對(duì)這次修改滿意的話锋爪,就用如下命令結(jié)束此次變基
git rebase --continue
重置賬戶郵箱信息
我們當(dāng)然要修改啦,那么執(zhí)行如下命令爸业,重置提交的賬戶信息:
git commit --amend --author="cmlanche <1204833748@qq.com>" --no-edit
同事其骄,要注意你的sourcetree,出現(xiàn)了新情況扯旷!
我們可以看到一個(gè)新的提交拯爽,并且,郵箱賬號(hào)都經(jīng)過了修改钧忽,如果你去掉--no-edit
還可以修改commit message毯炮,也就是圖中的"更新API",舉栗子吧耸黑,我可以繼續(xù)用amend修改此次變基
git commit --amend --author="cmlanche <1204833748@qq.com>"
保存退出vi編輯器桃煎,看sourcetree咋樣了:
真的很完美,接下來就是合并了大刊,退出變基为迈。
退出變基
git rebase --continue
在控制臺(tái)中打印如上命令退出變基,我們看到退出變基也就是使用最新的修改了缺菌,就一條分支了曲尸。
chengmingdeMacBook-Pro:server cmlanche$ git rebase --continue
Successfully rebased and updated refs/heads/bulma.
最后總結(jié)一下
變基真的很有用,他不是一條命令搞定的男翰,是一個(gè)過程另患,就像變成中打開了一個(gè)輸入流,最后用完你得關(guān)閉輸入流一樣蛾绎。
通過變基你可以輕松實(shí)現(xiàn)提交信息的任意重新修改昆箕!