配色: 字号:
利用rebase来压缩多次提交
2016-11-12 | 阅:  转:  |  分享 
  
利用rebase来压缩多次提交





我们可以用Gitmerge–squash来将分支中多次提交合并到master后,只保留一次提交历史。但是有些提交到github远程仓库中的commit信息如何合并呢?



历史记录

首先我们查看一下master分支的提交历史:



复制代码

$gitlog

commit415a0be986a48113829b3c60ee2387c6dbdc81d8

Author:xuxu

Date:MonJan2620:11:292015+0800



10->1



commited09a6cbe0797275ceead3f8c8d829d01f0e604b

Author:xuxu

Date:MonJan2619:57:172015+0800



2015.01.26



commit1821f6a1c1ed074fe886228cf33b3b3cb71819c4

Author:xuxu

Date:MonJan2619:53:282015+0800



2015.01.26



commitcc779982fc61e82ec494d6a5654417fa7194d748

Author:xuxu

Date:MonJan2619:40:242015+0800



2015.1.26

复制代码

我们看到上面有四次commit,如何将这个四次合并到一次呢?



压缩

使用下面的命令,最后一个数字4代表压缩最后四次提交。



gitrebase-iHEAD~4

该命令执行后,会弹出vim的编辑窗口,4次提交的信息会按照提交时间顺序排列,最上面的是最早一次提交,最下面的是最近一次提交。







我们需要修改第2-4行的第一个单词pick为squash,意思就是将下面的三次提交压缩到第一行中去,效果就是我们在pick所在的提交就已经做了4次动作,但是看起来就是一次而已。

注意:并不会删除下面的三次提交内容,只会将四次提交压缩为一次。











然后我们保存退出,git会一个一个压缩提交历史。



如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:



gitadd.

gitrebase--continue

如果你想放弃这次压缩的话,执行以下命令:



gitrebase--abort

如果所有冲突都已经解决了,会出现如下的编辑窗口:







这个时候我们需要修改一下合并后的commit的描述信息,我们将其描述为helloworld吧:







保存退出后会看到我们完整的信息:



复制代码

$gitrebase-iHEAD~4

[detachedHEAD9097684]helloworld

Author:xuxu

Committer:unknown

Yournameandemailaddresswereconfiguredautomaticallybased

onyourusernameandhostname.Pleasecheckthattheyareaccurate.

Youcansuppressthismesswww.wang027.comagebysettingthemexplicitly:



gitconfig--globaluser.name"YourName"

gitconfig--globaluser.emailyou@example.com



Afterdoingthis,youmayfixtheidentityusedforthiscommitwith:



gitcommit--amend--reset-author



9fileschanged,25insertions(+),11deletions(-)

Successfullyrebasedandupdatedrefs/heads/master.

复制代码

现在我们来查看一下历史:



复制代码

$gitlog

commit90976848524251b0a62376a9e45ea5c8aae25d87

Author:xuxu

Date:MonJan2619:40:242015+0800



helloworld



commitf57da9460196b950036fe4c2c8f7e4c9131ee04e

Author:xuxu

Date:MonJan2619:38:272015+0800



2015.01.26



commit64104e057f48d6dd0e432af8b55cbccd0a09ee63

Author:xuxu

Date:MonJan2619:31:422015+0800



2015.01.26



commit8499d4fa23c4395660d21e0b7dca873a7a3ef2b8

Author:xuxu

Date:MonJan2618:58:402015+0800



2015.01.26

复制代码

提交历史很清楚的显示出了我们的压缩成功了。



同步到远程仓库

github上的提交历史还是之前的,如何更新我们压缩后的历史记录呢?



我们采用gitpush试试:



复制代码

Tohttps://github.com/DoctorQ/AndroidTestScrpits.git

![rejected]master->master(non-fast-forward)

error:failedtopushsomerefsto''https://github.com/DoctorQ/AndroidTestScrpit

s.git''

hint:Updateswererejectedbecausethetipofyourcurrentbranchisbehind

hint:itsremotecounterpart.Integratetheremotechanges(e.g.

hint:''gitpull...'')beforepushingagain.

hint:Seethe''Noteaboutfast-forwards''in''gitpush--help''fordetails.

复制代码

被拒绝了,因为我们合并的历史记录已经在远程仓库之前了,你无法覆盖它。

那怎么办?如果你确定你的合并过程都是正确无误的,那么就可以强制push:



复制代码

$gitpush-f

warning:push.defaultisunset;itsimplicitvalueischangingin

Git2.0from''matching''to''simple''.Tosquelchthismessage

andmaintainthecurrentbehaviorafterthedefaultchanges,use:



gitconfig--globalpush.defaultmatching



Tosquelchthismessageandadoptthenewbehaviornow,use:



gitconfig--globalpush.defaultsimple



Whenpush.defaultissetto''matching'',gitwillpushlocalbranches

totheremotebranchesthatalreadyexistwiththesamename.



InGit2.0,Gitwilldefaulttothemoreconservative''simple''

behavior,whichonlypushesthecurrentbranchtothecorresponding

remotebranchthat''gitpull''usestoupdatethecurrentbranch.



See''githelpconfig''andsearchfor''push.default''forfurtherinformation.

(the''simple''modewasintrowww.baiyuewang.netducedinGit1.7.11.Usethesimilarmode

''current''insteadof''simple''ifyousometimesuseolderversionsofGit)



Countingobjects:1,done.

Writingobjects:100%(1/1),232bytes|0bytes/s,done.

Total1(delta0),reused0(delta0)

Tohttps://github.com/DoctorQ/AndroidTestScrpits.git

+415a0be...9097684master->master(forcedupdate)

复制代码

OK,不出意外的话,打开gitlab应该可以看到结果了。







crazyacking

AnITdeveloperfocusingonserverdevelopment.Astrongadvocateofopensource.

|Github|Facebook|Twitter|Google+|

献花(0)
+1
(本文系thedust79首藏)