简体   繁体   English

git:从所有历史记录中删除文件和目录,但保留所有分支(不仅是master分支)

[英]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.

相关问题 从远程Git仓库删除或删除所有历史记录,提交和分支? - Delete or remove all history, commits, and branches from a remote Git repo? git从所有分支删除文件和历史记录 - git remove file and history from all branches GIT:如何删除所有修补程序和功能分支,并自动保留开发,发布和主控? - GIT: How to delete all Hotfix and Feature branches, and keep Development, Releases and Master - automatically? 将所有文件从 master 移动到 Git 中的另一个分支 - Move all files from master to another branch in Git 将所有文件从master复制到git中的另一个分支 - Copy all files from master to another branch in git 删除git历史记录中的前x个提交,并删除其余历史记录中的所有合并分支 - delete first x commits in git history and remove all merge branches from the rest of the history 归档Github Master分支并删除Master分支中的所有文件 - Archive Github Master Branch and Delete All Files in Master Branch 压榨master分支的历史记录,但将所有先前的提交消息归咎于此? - Squash master branch history but keep all prior commit messages in blame? git fetch是否在所有分支上运行,还是仅在当前分支上运行? - Does git fetch operate on ALL branches or just the current branch? 如何删除除master之外的所有Git远程分支? - How to delete all Git remote branches except master?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM