如何删除除最后五个以外的所有Git提交 [英] How to delete all Git commits except the last five

查看:48
本文介绍了如何删除除最后五个以外的所有Git提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的Git存储库,其中仅包含经常更改的二进制文件.自然,Git存储库比其中的实际文件大得多.我并不真的在乎旧的历史,我只需要一些新的历史就可以恢复一些错误的更改.假设我要删除除最后五个提交以外的所有提交.

自然,我想这样做是为了使存储库较小,因此必须从存储库中完全清除已删除的提交.

我想用一个命令(别名)或脚本非交互地完成所有这些操作.我该怎么办?

解决方案

以下是 rebase-last-five 的别名,可以帮助您入门.它将重新创建当前分支,因此历史记录中只有最新的五个提交.最好使它成为一个脚本( git-rebase-last-five.sh ),该脚本可在 PATH 上的目录中找到;Git将查找并使用名为 git -.... sh 的脚本,而无需进行任何特殊配置.该脚本应该比这个简单的别名执行更多的错误检查和处理.

  $ git config --global alias.rebase-last-five'!b ="$(git branch --no-color | cut -c3-)";h ="$(git rev-parse $ b)";echo当前分支:$ b $ h";c ="$(git rev-parse $ b〜4)";echo使用初始提交$ c重新创建$ b分支...";git checkout --orphan新开始$ c;git commit -C $ c;git rebase-到新的开始$ c $ b;git branch -d new-start;git gc' 

加薪者:请注意有关更改历史记录的警告./p>

检查 man 页面( git help< command> 在线)以获取更多信息.

用法示例:

  $ git --versiongit版本1.7.12.rc2.16.g034161a$ git log --all --graph --decorate --oneline* e4b2337(HEAD,master)9* e508980 8* 01927dd 7* 75c0fdb 6* 20edb42 5* 1260648 4* b3d6cc8 3* 187a0ef 2* e5d09cf 1* 07bf1e2初始$ git rebase-last-five当前分支:主站e4b2337ef33d446bbb48cbc86b44afc964ba0712使用初始提交20edb42a06ae987463016e7f2c08e9df10fd94a0重新创建master分支...切换到新分支"new-start"[new-start(root-commit)06ed4d5] 5更改了1个文件,插入了1个(+)创建模式100644 A首先,快退一下,在上面重播您的作品...申请:6申请:7申请:8申请:9删除了分支的新起点(原为06ed4d5).计数对象:35,完成.最多2个线程的增量压缩.压缩对象:100%(15/15),已完成.书写对象:100%(35/35),已完成.总计35(增量4),已重用0(增量0)$ git log --all --graph --decorate --oneline* a7fb54b(HEAD,master)9* 413e5b0 8* 638a1ae 7* 9949c28 6* 06ed4d5 5 

I have a very large Git repository which only contains binary files which are changed pretty often. Naturally the Git repository is much larger than the actual files in it. I don't really care about old history, I only need some of the newer history to be able to revert some wrong changes. So let's say I want to remove all commits except the last five.

Naturally I want to do this to keep the repository small so the removed commits must be purged completely from the repo.

And I want to do all this non-interactively with a single command (alias) or script. How can I do this?

解决方案

Here's an rebase-last-five alias to get you started. It will recreate the current branch so only the most recent five commits are in the history. It's probably best to make this a script (git-rebase-last-five.sh) that is available in a directory on your PATH; Git will find and use scripts named git-....sh without the need for any special configuration. The script should do more error checking and handling than this simple alias.

$ git config --global alias.rebase-last-five '!b="$(git branch --no-color | cut -c3-)" ; h="$(git rev-parse $b)" ; echo "Current branch: $b $h" ; c="$(git rev-parse $b~4)" ; echo "Recreating $b branch with initial commit $c ..." ; git checkout --orphan new-start $c ; git commit -C $c ; git rebase --onto new-start $c $b ; git branch -d new-start ; git gc'

CAVEAT EMPTOR: Do heed the warnings about changing history.

Check the man pages (git help <command> or online) for further information.

An example usage:

$ git --version
git version 1.7.12.rc2.16.g034161a
$ git log --all --graph --decorate --oneline
* e4b2337 (HEAD, master) 9
* e508980 8
* 01927dd 7
* 75c0fdb 6
* 20edb42 5
* 1260648 4
* b3d6cc8 3
* 187a0ef 2
* e5d09cf 1
* 07bf1e2 initial
$ git rebase-last-five 
Current branch: master e4b2337ef33d446bbb48cbc86b44afc964ba0712
Recreating master branch with initial commit 20edb42a06ae987463016e7f2c08e9df10fd94a0 ...
Switched to a new branch 'new-start'
[new-start (root-commit) 06ed4d5] 5
 1 file changed, 1 insertion(+)
 create mode 100644 A
First, rewinding head to replay your work on top of it...
Applying: 6
Applying: 7
Applying: 8
Applying: 9
Deleted branch new-start (was 06ed4d5).
Counting objects: 35, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (35/35), done.
Total 35 (delta 4), reused 0 (delta 0)
$ git log --all --graph --decorate --oneline
* a7fb54b (HEAD, master) 9
* 413e5b0 8
* 638a1ae 7
* 9949c28 6
* 06ed4d5 5

这篇关于如何删除除最后五个以外的所有Git提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆