Git权威指南--改变历史

2.多步悔棋

0.查看版本库最新五次提交

$ git log --stat --oneline -5
e2609ca 加结束标志
 test_git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
1f5c128 此处省略一万字
 test_git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
306b97b 增加修改时间
 test_git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
0c30d3e 增加修改人
 test_git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
07cab98 新增测试文件
 test_git.txt | 1 +
 1 file changed, 1 insertion(+)

将最近两次提交合二为一,并把提交说明改为“增加修改时间......”

1.使用--soft 参数 调用重置命令,回到最近两次提交之前

$ git reset --soft HEAD^^

2.查看版本库最新提交

$ git log -1
commit 306b97b5dc629cb428d664c50f31f7815ad370f0
Author: yinnana <nanayin@creditease.cn>
Date:   Wed Feb 8 13:14:10 2017 +0800

    增加修改时间

3.执行提交操作,即完成最新两个提交压缩为一个提交的操作

$ git commit -m "增加修改时间......"
[master 060a9f4] 增加修改时间......
 1 file changed, 3 insertions(+), 1 deletion(-)

4.查看提交日志,验证

$ git log --stat --oneline -5
060a9f4 增加修改时间......
 test_git.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
306b97b 增加修改时间
 test_git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
0c30d3e 增加修改人
 test_git.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
07cab98 新增测试文件
 test_git.txt | 1 +
 1 file changed, 1 insertion(+)
66a5a9b modify test.txt
 test.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)


3.回到未来


涉及到的:拣选操作、变基操作、交互式变基操作

3.1  去除提交 D

0)对提交进行标识

$ git tag F
$ git tag E HEAD^
$ git tag D HEAD~2
$ git tag C HEAD~3
$ git tag B HEAD~4
$ git tag A HEAD~5
通过日志,可以看到被标记的六个提交

$ git log --oneline --decorate -6
2cbfc1b (HEAD -> master, tag: F) 添加正文
e869230 (tag: E) 编辑文本内容
6080809 (tag: D) 删除 注意事项!!
f94e582 (tag: C) 注意事项1
af8a1c5 (tag: B) 添加 注意事项
060a9f4 (tag: A) 增加修改时间......

1)执行git checkout ,暂时将HEAD头指针切换到C(切换过程中显示出于非跟踪状态的警告)

$ git checkout C
Note: checking out 'C'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at f94e582... 注意事项1
$ git log --oneline --decorate -6
f94e582 (HEAD, tag: C) 注意事项1
af8a1c5 (tag: B) 添加 注意事项
060a9f4 (tag: A) 增加修改时间......
306b97b 增加修改时间
0c30d3e 增加修改人
07cab98 新增测试文件

2)执行拣选操作将E提交在当前HEAD上重放。

因为E和master^显然指向同一指向,因此可以用如下语法。

$ git cherry-pick master^
[detached HEAD d8862cb] 编辑文本内容
 Date: Wed Feb 8 13:32:28 2017 +0800
 1 file changed, 1 insertion(+), 1 deletion(-)

3)执行拣选操作将F提交在当前HEAD上重放

F和master指向同一指向

$ git cherry-pick master
[detached HEAD 5883cdd] 添加正文
 Date: Wed Feb 8 13:33:14 2017 +0800
 1 file changed, 3 insertions(+)

4)通过日志看到D不存在了

$ git log --oneline --decorate -6
5883cdd (HEAD) 添加正文
d8862cb 编辑文本内容
f94e582 (tag: C) 注意事项1
af8a1c5 (tag: B) 添加 注意事项
060a9f4 (tag: A) 增加修改时间......
306b97b 增加修改时间

5)通过日志可以看出,最近两次提交的原始创作日期(AuthorDate)和提交日期(CommitDate)不同。

AuthorDate是拣选提交的原始更改时间,CommitDate是拣选操作的时间

$ git log --pretty=fuller --decorate -3
commit 5883cddb3b5d8f5a88aec7eb1ce1ff616a9606e4 (HEAD)
Author:     yinnana <nanayin@creditease.cn>
AuthorDate: Wed Feb 8 13:33:14 2017 +0800
Commit:     yinnana <nanayin@creditease.cn>
CommitDate: Wed Feb 8 14:00:40 2017 +0800

    添加正文

commit d8862cbb1913ced14ce4c9441204019965751567
Author:     yinnana <nanayin@creditease.cn>
AuthorDate: Wed Feb 8 13:32:28 2017 +0800
Commit:     yinnana <nanayin@creditease.cn>
CommitDate: Wed Feb 8 13:58:16 2017 +0800

    编辑文本内容

commit f94e582809f7cc05bf5e8c9950dd32ebc4ac6ac4 (tag: C)
Author:     yinnana <nanayin@creditease.cn>
AuthorDate: Wed Feb 8 13:30:53 2017 +0800
Commit:     yinnana <nanayin@creditease.cn>
CommitDate: Wed Feb 8 13:30:53 2017 &
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值