论优雅地使用 Git ——修改历史

以下内容都是针对没有push之前的操作,push之后要修改就比较麻烦了。

再提一点题外话,帅气的敲完一行命令,回车之前请冷静一秒钟。

修改上一次的提交说明

这是一个很常见的需求,打错字了什么的,强迫症是绝对不能忍的。

git commit --amend

会打开一个编辑器,可以在里面修改最后一次的提交说明,保存并退出即可。

修改上一次提交,增加一个文件

git add你刚才没提交的文件(当然这里还可以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,确认无误之后,如有必要修改提交说明,这里我增加一行“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这个文件,然后把这个提交和上一个合并。

另外,在开发过程中,会有无数无意义的commit,比如重构的时候,我喜欢改一点就提交,每次提交变化都不大,review code的效果比较好,有助于保证开发质量;比如 git commit -m "回家再写",但是测试通过合并到master的时候,以项目视角来看,这些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,其意思是“压缩”。当然,如果是三个提交合并,就把后两个pick改为squash

Commands提示了这里还可以进行的操作,你有需要可以自己研究一下。

修改之后,保存退出,进入下一步,修改提交说明

# 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.

你可能感兴趣的:(论优雅地使用 Git ——修改历史)