GIT压缩多次提交记录为一次

GIT压缩多次提交记录为一次

创建文件夹并进行Git初始化
$ mkdir test-rebase
$ cd test-rebase 
$ git init
Initialized empty Git repository in /Users/apple/Develop/git/test-rebase/.git/
添加四次提交记录
# 第一次提交
$ touch a.txt
$ git add .
$ git commit -m 'a'
[master (root-commit) 6bf1717] a
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
# 第二次提交
$ touch b.txt
$ git add .
$ git commit -m 'b'
[master 356bfff] b
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b.txt
# 第三次提交
$ touch c.txt 
$ git add .
$ git commit -m 'c'
[master e5e5c05] c
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c.txt
# 第四次提交
$ touch d.txt
$ git add .
$ git commit -m 'd'
[master edaf5da] d
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 d.txt
查看日志
$ git log
commit 08b1b469a8ed1175013a181f1090521c6d1f126a (HEAD -> master)
Author: xf <x17301932065@163.com>
Date:   Fri Apr 23 16:09:28 2021 +0800

    d

commit 76ce2e10599f86849d4e602f218322185d4e5ed1
Author: xf <x17301932065@163.com>
Date:   Fri Apr 23 16:09:16 2021 +0800

    c

commit 197075cddc4e0a21356ea29cdb39f7f9b9eb39b0
Author: xf <x17301932065@163.com>
Date:   Fri Apr 23 16:09:07 2021 +0800

    b

commit 52389daeceb1e4747b58e6f87a38239c578f633e
Author: xf <x17301932065@163.com>
Date:   Fri Apr 23 16:08:58 2021 +0800

    a
将b到d压缩为一次提交
$ git rebase -i 52389daeceb1e4747b58e6f87a38239c578f633e 08b1b469a8ed1175013a181f1090521c6d1f126a

第一个COMMIT记录id为压缩前的一次记录id,第二个COMMIT记录id为要压缩到的记录id。这里就是将从a过后到d的提交记录进行压缩,即压缩b、c、d提交记录。

执行命令后会弹出如下VIM编辑界面:

pick 197075c b
pick 76ce2e1 c
pick 08b1b46 d

# Rebase 6bf1717..edaf5da onto 6bf1717 (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
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
"~/Develop/git/test-rebase/.git/rebase-merge/git-rebase-todo" 29L, 1181C

修改记录,这里保留最后一次提交记录,所以将其改为:

pick 197075c b
s 76ce2e1 c
s 08b1b46 d

保存后结果为:

# This is a combination of 3 commits.
# This is the 1st commit message:

b

# This is the commit message #2:

c

# This is the commit message #3:

d

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Apr 23 16:09:07 2021 +0800
#
# interactive rebase in progress; onto 52389da
# Last commands done (3 commands done):
#    squash 76ce2e1 c
#    squash 08b1b46 d
# No commands remaining.

再次修改,使提交记录日志为我们修改的日志(也可以不修改):

# This is a combination of 3 commits.
# This is the 1st commit message:

b-压缩后提交记录

# This is the commit message #2:

# c

# This is the commit message #3:

# d

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Apr 23 16:09:07 2021 +0800
#
# interactive rebase in progress; onto 52389da
# Last commands done (3 commands done):
#    squash 76ce2e1 c
#    squash 08b1b46 d
# No commands remaining.
查看当前分支

保存上面提交后,查看分支信息,当前分支并不在master上了,这个分支并不会保存,只是我们压缩提交记录后的一个临时分支:

$ git branch
* (HEAD detached from refs/heads/master)
  master

创建新分支

基于当前临时分支创建新分支:

git checkout -b new
Switched to a new branch 'new'

查看日志:

git log
commit 3d9e6dabd859310e0c44de3ccbe241e2b7098a01 (HEAD -> new)
Author: ****
Date:   Fri Apr 23 16:09:07 2021 +0800

    b-压缩后提交记录

commit 52389daeceb1e4747b58e6f87a38239c578f633e
Author: ****
Date:   Fri Apr 23 16:08:58 2021 +0800

    a

可以看到提交记录就是合并后的记录了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值