Git 小结

已经add或者commit(但未push),想撤销

git reset --hard HEAD^
所有的改动都被擦掉了,就像一切没有发生过:包括已经commit但未push的,已经add但未commit的。

git reset --soft HEAD^
撤销commit,但add未撤销。

git reset HEAD^
git reset --mixed HEAD^
撤销commit、add。在Idea工具下,修改的文件都呈红色。

已经push了,但想撤销那次push

找到commit_id,形如:126f206185f225879f2723ca421f4dee44ca8fe7

然后执行:
git reset --hard 126f206185f225879f2723ca421f4dee44ca8fe7
git push origin HEAD --force

摘樱桃,选大而红的摘到自己篮子里

张三在分支dev_zs上有一次提交,commit id为9bf9c795e1c788704832b4b200279e240ee8a3a6。
我在分支dev_me上开发。我觉得张三的那次提交可以为我所用,于是:
git cherry-pick 9bf9c795e1c788704832b4b200279e240ee8a3a6

本地有多次commit(但未push),看着太乱,想合并commit注释

在idea 2019.1的终端里依次执行:

git log
Meaningless commit-03  <== 第三次commit的内容
Meaningless commit-02  <== 第二次commit的内容
Meaningless commit-01  <== 第一次commit的内容

1、想合并最近的3次提交纪录

git rebase -i HEAD~3

稍等片刻,将进入vi效果页面:

pick 95e5514 Meaningless commit-01
pick a98bd43 Meaningless commit-02
pick ffc49db Meaningless commit-03

# Rebase 575598e..ffc49db onto 575598e (3 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.
#

单击字母I键,进入vi编辑效果,将前3行编辑为:

pick 95e5514 Meaningless commit-01
s a98bd43 Meaningless commit-02
s ffc49db Meaningless commit-03

单击ESC键,进入vi命令效果,保存退出,键入:wq
页面出现以下内容:

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

Meaningless commit-01

# This is the commit message #2:

Meaningless commit-02

# This is the commit message #3:

Meaningless commit-03

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Jul 19 11:21:46 2019 +0800
#
# interactive rebase in progress; onto 575598e
# Last commands done (3 commands done):
#    squash a98bd43 Meaningless commit-02

单击字母I键,进入vi编辑效果,在Meaningless commit-02和Meaningless commit-03行首输入#号,将其注释掉,即不要了。编辑Meaningless commit-01那行,修改为有意义的内容,如:Merged meaningful Message.
整体效果如下:

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

Merged meaningful Message.

# This is the commit message #2:

# Meaningless commit-02

# This is the commit message #3:

# Meaningless commit-03

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Jul 19 11:21:46 2019 +0800
#
# interactive rebase in progress; onto 575598e
# Last commands done (3 commands done):
#    squash a98bd43 Meaningless commit-02

单击ESC键,进入vi命令效果,保存退出,键入:wq
操作完成,看下git log,仅剩下:

git log
Merged meaningful Message.

如果你异常退出了 vi 窗口,不要紧张:

git rebase --edit-todo

merge与rebase

先假设一个工作场景:

  1. 小金新建了一个项目,经过多天的开发,第一次上传到Git库,分支名称假定为master。
  2. 小金新建了一个dev分支,一人在其上开发新功能,估计时间挺长的。
  3. 小孟在master分支上修改了一个Bug,commit并push到master分支。
  4. 小金在dev上奋笔疾书,两耳不闻窗外事,可能也会commit几次,但未push。
  5. 过了几天,小孟在master分支上又修改了一个Bug,commit并push到master分支。
  6. 小金还在开发中,他想把小孟的提交“整合到”dev分支,免得后期合并到master时,冲突太多,他有两个选择:a)merge操作,b)rebase操作。

merge操作

Merge操作经常会生成类似于“Merge remote-tracking branch ‘origin/master’ into dev”的git log。如果你有git log洁癖,可以考虑rebase。

rebase操作

按上面的工作场景描述,小金实际上是从master的第一个commit上建的分支,但rebase操作完成后,仿佛是从小孟的第近一次commit上建的分支。
在这里插入图片描述

在rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决 冲突;在解决完冲突后,用"git add"命令去更新这些内容的索引(index), 然后,你无需执行 git commit,只要执行:
$ git rebase --continue
这样git会继续应用(apply)余下的补丁。

在任何时候,你可以用–abort参数来终止rebase的行动,并且"mywork" 分支会回到rebase开始前的状态。
$ git rebase --abort

参考资料

https://www.cnblogs.com/marblemm/p/7161614.html
http://jartto.wang/2018/12/11/git-rebase/
http://gitbook.liuhui998.com/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值