前言
“嘀嗒嘀嗒”,抬頭看向牆上的鍾表,此時已是凌晨1點。小明終於把Go語言聖經第二章的筆記寫完,保存commit,提交,然后睡覺。
額,等等,不對,小明發現他用的是公司的git賬號,git log一看,最新的commit的Author信息里是公司的郵箱地址,尷尬了,難道小明要重新寫一遍?“不要啊~”,小明抓狂到。
突然,畫面暫停,Git博士從幕后走出,原來是一場電影。Git博士說:“同學們,剛才的案例如果是大家遇到,應該怎樣?”,接着說:“不要慌,git rebase幫你解決”
正文
先看一下小明的日志
$git log
commit 34da55544be6ceb1269e24b921275b4a771 (HEAD -> main, origin/main, origin/HEAD)
Author: Company <conpany@company.com>
Date: Mon Jun 8 01:51:52 2021 +0800
commit 3
commit 98f419726756cba7923e3c0062bd1231d25
Author: Ming <ming@ming.com>
Date: Mon Jun 7 20:09:48 2021 +0800
commit 2
commit 15f0d5882db5dedee737a90e3df98bd395
Author: Company <conpany@company.com>
Date: Sat May 15 16:34:06 2021 +0800
commit 1
小明的commit 1
和commit 3
寫錯了Author信息Company <conpany@company.com>
,我們的目標是將這兩個commit的作者改成Ming <ming@ming.com>
,讓小明早點休息,明天還要去搬磚~
修改上次commit的Author信息
$git commit --commit --author="Ming <ming@ming.com>"
進入一個類似vim編輯器的交互頁
commit 3
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author: ming <ming@ming.com>
# Date: Mon Jun 7 22:05:18 2021 +0800
#
# On branch master
#
# On branch main
#
# Changes to be committed:
# new file: ...
#
保存退出即可
修改以前的commit的Author信息
輸入下面的命令,-i參數表示交互方式
git rebase -i HEAD~3
進入一個類似vim編輯器的交互頁,將要修改的commit 1
開頭的pick
改成edit
edit acdaa07 commit 1
pick 2f30e03 commit 2
pick 34da555 commit 3
# Rebase b94735d..34da555 onto b94735d (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
然后保存退出,即:wq
,輸出下面的下面的信息:
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
接着輸入下面的命令來修改提交者信息
$git commit --amend --author="Ming <ming@ming.com>"
最后輸入保存命令
$git commit --continue
Successfully rebased and updated refs/heads/master.`
我們再來看下小明的日志
$git log
commit 34da55544be6ceb1269e24b921275b4a771 (HEAD -> main, origin/main, origin/HEAD)
Author: Ming <ming@ming.com>
Date: Mon Jun 8 01:51:52 2021 +0800
commit 3
commit 98f419726756cba7923e3c0062bd1231d25
Author: Ming <ming@ming.com>
Date: Mon Jun 7 20:09:48 2021 +0800
commit 2
commit 15f0d5882db5dedee737a90e3df98bd395
Author: Ming <ming@ming.com>
Date: Sat May 15 16:34:06 2021 +0800
commit 1
大功告成,小明終於可以洗洗睡了,明天今天醒來又是美好的一天~