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


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

假設我們的 commit tree 如下:

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

接下來要移除 B 跟 C 的 commit tree,變成

R–A–D’–E–HEAD

有兩種方式可以移除 B & C

# detach head and move to D commit
git checkout <SHA1-for-D>

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

# Redo the D commit re-using the commit message, but now on top of A
git commit -C <SHA1-for-D>

# Re-apply everything from the old D onwards onto this new place
git rebase --onto HEAD <SHA1-for-D> master

# push it
git push --force
另一種方法是利用 cherry-pick 方式

git rebase --hard <SHA1 of A>
git cherry-pick <SHA1 of D>
git cherry-pick <SHA1 of E>
這會直接忽略 B 跟 C 的 history,詳細資料可以查詢 git help cherry-pick 或者是 git help rebase

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值