以下內(nèi)容都是針對沒有push之前的操作受扳,push之后要修改就比較麻煩了琐簇。
再提一點(diǎn)題外話,帥氣的敲完一行命令头朱,回車之前請冷靜一秒鐘运悲。
修改上一次的提交說明
這是一個很常見的需求,打錯字了什么的项钮,強(qiáng)迫癥是絕對不能忍的班眯。
git commit --amend
會打開一個編輯器希停,可以在里面修改最后一次的提交說明,保存并退出即可鳖敷。
修改上一次提交脖苏,增加一個文件
先 git add
你剛才沒提交的文件(當(dāng)然這里還可以git rm
刪掉你剛才錯誤提交的文件等等)
然后git commit --amend
, 在編輯器里面看到
add newfile.txt for testing
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Nov 11 14:25:39 2017 +0800
#
# On branch master
# Changes to be committed:
# new file: last.txt
# new file: newfile.txt
#
第一行是之前的提交說明,最后幾行是修改后的commit定踱,先檢查一下commit,確認(rèn)無誤之后恃鞋,如有必要修改提交說明崖媚,這里我增加一行“add lost.txt”,保存退出恤浪,最后一次提交即被修改
$ git log -1
commit e8f84c4594c3492d98845f65c7483ddcf5bd37d4 (HEAD -> master)
Author: ***
Date: Sat Nov 11 14:25:39 2017 +0800
add newfile.txt for testing
add lost.txt
合并歷史提交
忘提交文件的問題畅哑,除了修改上一次的提交,還可以直接commit這個文件水由,然后把這個提交和上一個合并荠呐。
另外,在開發(fā)過程中砂客,會有無數(shù)無意義的commit泥张,比如重構(gòu)的時候,我喜歡改一點(diǎn)就提交鞠值,每次提交變化都不大媚创,review code的效果比較好,有助于保證開發(fā)質(zhì)量彤恶;比如 git commit -m "回家再寫"
,但是測試通過合并到master的時候钞钙,以項(xiàng)目視角來看,這些commtis是多余的声离,這時候我們就可以在合并到master之前把這些commit合并為一個git commit-m "refactor: ..."
這里有兩個commit:
$ git log -2
commit 46104f1e69b2858e3b915b0c30e90b39dd0a96b4 (HEAD -> master)
Author: ***
Date: Sat Nov 11 15:24:44 2017 +0800
finished
commit 3f82622b2b0f4628f8608499b4fa9038c09fad85
Author: ***
Date: Sat Nov 11 15:23:52 2017 +0800
回家再寫
我們使用git rebase -i
以交互的方式來修改芒炼。
要修改的是最近兩次的提交,合并為一個术徊。
$ git rebase -i HEAD~2
顯示:
pick 3f82622 回家再寫
pick 46104f1 finished
# Rebase e8f84c4..46104f1 onto e8f84c4 (2 commands)
#
# 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
需要注意的是這里提交歷史的順序和git log
是相反的本刽。
我們要把兩個提交合并為一個,因此是將第二個 pick
改為squash
弧关,其意思是“壓縮”盅安。當(dāng)然,如果是三個提交合并世囊,就把后兩個pick
改為squash
别瞭。
Commands
提示了這里還可以進(jìn)行的操作,你有需要可以自己研究一下株憾。
修改之后蝙寨,保存退出晒衩,進(jìn)入下一步,修改提交說明
# This is a combination of 2 commits.
# This is the 1st commit message:
回家再寫
# This is the commit message #2:
finished
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Nov 11 15:23:52 2017 +0800
#
# interactive rebase in progress; onto e8f84c4
# Last commands done (2 commands done):
# pick 3f82622 回家再寫
# squash 46104f1 finished
# No commands remaining.
# You are currently rebasing branch 'master' on 'e8f84c4'.
#
# Changes to be committed:
# new file: file.txt
#
如有需要墙歪,修改提交說明听系,這里我修改為
finished
# Please enter the commit message for your changes. Lines starting
...
保存退出,提示操作完成虹菲。
$ git rebase -i HEAD~2
[detached HEAD 45d77b7] finished
Date: Sat Nov 11 15:23:52 2017 +0800
1 file changed, 3 insertions(+)
create mode 100644 file.txt
Successfully rebased and updated refs/heads/master.
查看一下歷史
$ git log -2
commit 45d77b75eb7a5fd44cc3313df893195d9a4db905 (HEAD -> master)
Author: ***
Date: Sat Nov 11 15:23:52 2017 +0800
finished
commit e8f84c4594c3492d98845f65c7483ddcf5bd37d4
Author: ***
Date: Sat Nov 11 14:25:39 2017 +0800
add newfile.txt for testing
add lost.txt
沒問題靠胜。
這里只說了幾個單純場景下的對歷史提交的操作,更全面的使用請自行Google.