[英]git: Delete files and directories from all history but keep all branches (not just the master branch)
I tried this tutorial to delete some sensitive files and directories from my Git history. 我试图通过本教程从Git历史记录中删除一些敏感文件和目录。 However, at the end, my new repository only had 1 branch: the master branch.
但是,最后,我的新存储库只有一个分支:master分支。
How is possible to delete files and directories from Git history while keeping all branches? 在保留所有分支的同时,如何从Git历史记录中删除文件和目录?
Thank you! 谢谢!
commands I used (from the tutorial): 我使用的命令(来自教程):
cd /tmp
git clone https://MY_GIT_REPOSITORY.git workingrepo
cd workingrepo
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch FILE_LIST' --prune-empty -f -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --aggressive --prune=now
From what you've posted, that should have worked. 从您发布的内容来看,这应该有效。 The:
该:
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do git branch --track ${branch##*/} $branch done
sequence should create a branch for each branch in the original repository. 序列应为原始存储库中的每个分支创建一个分支。 If you examine the set of branches afterward, you should see the right names.
如果以后检查分支集,则应该看到正确的名称。 (This method is a bit clumsy compared to just using
git clone --mirror
for the initial clone, but sometimes there are reasons to avoid --mirror
, which makes a bare clone.) (与仅使用
git clone --mirror
进行初始克隆相比,此方法有点笨拙,但有时有必要避免使用--mirror
来进行裸克隆。)
The filter-branch operation (lines split for posting purposes here): 筛选器分支操作(在此处为发布目的而拆分的行):
git filter-branch --tag-name-filter cat \\ --index-filter 'git rm -r --cached --ignore-unmatch FILE_LIST' \\ --prune-empty -f -- --all
should then operate on all branches ( --all
at the end); 然后应在所有分支(操作
--all
末); the index filter will remove a file named FILE_LIST
(presumably this is short for the actual list of files), and you should see a lot of progress messages plus a list of branch names that filter-branch
updated. 索引过滤器将删除一个名为
FILE_LIST
的文件(假定这是实际文件列表的缩写),您应该会看到许多进度消息以及filter-branch
更新的分支名称列表。
You can examine the branch names again at this point; 此时,您可以再次检查分支名称。 they should be the same names as before (though the commit hashes to which they point may, and usually will, differ).
它们的名称应与以前相同(尽管它们指向的提交散列可能并且通常会有所不同)。
The remaining commands do some cleanup. 其余命令进行一些清理。 The
--aggressive
is unnecessary (see Linus Torvalds' explanation here ) but using git gc
or re-cloning are appropriate for discarding the unwanted objects. --aggressive
是不必要的(请参阅此处的Linus Torvalds的说明 ),但是使用git gc
或重新克隆对于丢弃不需要的对象是合适的。 These will have no effect on branch names, so those two particular points are where to check why something has gone wrong. 这些将不会对分支名称产生影响,因此这两个特定点是检查错误原因的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.