从Git存储库中的历史记录中修剪空合并提交

时间:2012-03-21 11:03:03

标签: git git-filter-branch

我已经清理了我们的Git存储库,我们需要删除历史记录中的大部分内容。我是这样做的:

git filter-branch --prune-empty --tree-filter 'rm -rf some_stuff'

--prune-empty标志将删除在进程后保留为空的提交,除了提交多个父项(合并提交)。即使合并的分支完全没有包含任何内容,合并也不会向树添加任何内容。

如何从历史记录中修剪这些空的合并提交?

5 个答案:

答案 0 :(得分:31)

优于rebase解决方案,因为它保留原始历史记录的提交者信息,提交者日期和非空合并

git filter-branch --prune-empty --parent-filter \
    'sed "s/-p //g" | xargs -r git show-branch --independent | sed "s/\</-p /g"'

这受到与卢卡斯解决方案相同的thread on the kernel mailing list的启发。然而,它不需要Ruby并且是单行的。它确实需要GNU版本的xargssed

答案 1 :(得分:11)

我需要在ssokolow/profile的副本上运行filter-branch后将ssokolow/lap分开。

这做了一个不错的工作,因为自动“崩溃了任何遗留下来的--prune-empty”命令:

git rebase --root HEAD

(我需要--root所以它会用最旧的仍然有内容的初始提交替换现在空的初始提交。)

答案 2 :(得分:3)

这看起来对我有用:http://git.661346.n2.nabble.com/Removing-useless-merge-commit-with-quot-filter-branch-quot-td7356544.html

(defn fib-step [[x y]] [x [y (+ x y)]])
(take 10 (unfold fib-step [0 1])) ;=> (0 1 1 2 3 5 8 13 21 34)

(defn decreasing [x] (if (neg? x) nil [x (dec x)]))
(unfold decreasing 5) ;=> (5 4 3 2 1 0)

git filter-branch -f --prune-empty --parent-filter FULL_PATH_TO/rewrite_parent.rb master

rewrite_parent.rb

答案 3 :(得分:0)

所以我的解决方案是将所有内容移至sub-dir(历史上),然后移动--subdirectory-filter

第一步error combining git repositories into subdirs

我修改了一下sh文件:

#!/bin/bash

git ls-files -s | sed "s-\t-&temp_dir/-" | GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info

mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" || true

运行

git filter-branch --index-filter '~/doit.sh' HEAD

第二步

git filter-branch --subdirectory-filter temp_dir --prune-empty

然后推它。

答案 4 :(得分:-2)

我在filter-branch周围发现了一个非常可爱的小包装脚本:

https://github.com/pflanze/chj-bin/blob/master/cj-git-filter-branch

这里的前提和背景:

http://lists.q42.co.uk/pipermail/git-announce/2011-September/000486.html

相关问题