推荐给好友 上一篇 | 下一篇

how to remove file and commit from history(如何移除 commit 歷史紀錄)

BSD爱好者乐园Iu]K+L`

今天在 commit 程式碼到 github 網站,不小心把 Plurk 帳號密碼給 commit 上去,發生這種事情,所以趕快上網查了一下如何移除 commit 歷史紀錄:

OG5LMxpKSI

AranbD假設我們的 commit tree 如下:BSD爱好者乐园.Df4d:b9c8Fe1AL

BSD爱好者乐园C MB_Ift

R–A–B–C–D–E–HEAD

Ks-K:q\9XBWxBSD爱好者乐园#lw?+W }Y0fsy%zW

接下來要移除 B 跟 C 的 commit tree,變成BSD爱好者乐园 n&n4?3Ga!^ c-P u$b@"C

BSD爱好者乐园}B'{N?Y3`:r

R–A–D’–E–HEAD

v*D:T$bI |"`0[

,z;k8e,Wf ^4V1T/s t"N有兩種方式可以移除 B & CBSD爱好者乐园M[9z W"nLubb

k-HBTt# detach head and move to D commitBSD爱好者乐园0I k~D;Y`
git checkout <SHA1-for-D>BSD爱好者乐园z4k)w7c7rNX7@

BSD爱好者乐园j K0_&J;ry

# move HEAD to A, but leave the index and working tree as for D
GwW"Orzgit reset --soft <SHA1-for-A>

`P(XM_EBSD爱好者乐园t4@HF-_bu3V

# Redo the D commit re-using the commit message, but now on top of A
j9qQ_,~git commit -C <SHA1-for-D>BSD爱好者乐园Cf qL&w

'k+P |?$R\# Re-apply everything from the old D onwards onto this new place
[^#PM'W,X2ugit rebase --onto HEAD <SHA1-for-D> master

!B,L B.n)E

n1x6Oi#P9F"ky# push it
/^L |/pM0Tgit push --forceBSD爱好者乐园bbCpe8V#L
另一種方法是利用 cherry-pick 方式BSD爱好者乐园M9c7Hb)GH;s

h*@7p%t0k@7vlgit rebase --hard <SHA1 of A>BSD爱好者乐园2KR[Yfsd
git cherry-pick <SHA1 of D>BSD爱好者乐园xqpiHR+iujXj
git cherry-pick <SHA1 of E>
z_g%jjV'f])U這會直接忽略 B 跟 C 的 history,詳細資料可以查詢 git help cherry-pick 或者是 git help rebase

vIw+l0U A"g
[版权声明]BSD爱好者乐园站内文章,如来源不是互联网,则均系原创或翻译之作,可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。
TAG: Commit commit 记录
 

评分:0

我来说两句

seccode