从git repo中删除'.git'目录?

dan*_*ana 22 git mercurial git-filter-branch git-rewrite-history

我正在尝试将git repo从Kiln迁移到Github.我可以添加新的遥控器,但是当我尝试将master推送到新的遥控器时,我收到以下错误:

Counting objects: 8691, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3000/3000), done.
remote: error: object a9ee490ac00987835de30bdbc851da5e8d45d28b:contains '.git'
remote: fatal: Error in object
error: pack-objects died of signal 13
error: failed to push some refs to 'git@github.com:Account/repo.git'
Run Code Online (Sandbox Code Playgroud)

Commit中a9ee490ac00987835de30bdbc851da5e8d45d28b包含以下文件:

.git/
CHANGELOG.md
JSONKit.h
JSONKit.m
README.md
Run Code Online (Sandbox Code Playgroud)

显然有人在过去使用hg,在子目录中检查完整的git仓库.

我想完全杀死该目录,但是从git历史记录中删除该文件时遇到问题.

推送git repo的答案失败了,错误:包含'.git'没有帮助,因为我将repo作为git repo,而不是mercurial repo.

我试过了git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch .git',但是我得到了错误:

Rewrite 7dbd0970d6c79215d11994b4a9b8091b2e954cfb (326/442)error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/HEAD'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/config'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/description'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/applypatch-msg.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/commit-msg.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/post-update.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/pre-applypatch.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/pre-commit.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/pre-rebase.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/prepare-commit-msg.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/hooks/update.sample'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/index'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/info/exclude'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/logs/HEAD'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/logs/refs/heads/master'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/objects/pack/pack-43fac03d375df5c1e380c5e522ba6bcb9b4e1ec1.idx'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/objects/pack/pack-43fac03d375df5c1e380c5e522ba6bcb9b4e1ec1.pack'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/packed-refs'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/refs/heads/master'
error: Invalid path 'Account/Libraries/ShareKit/Submodules/JSONKit/.git/refs/remotes/origin/HEAD'
Run Code Online (Sandbox Code Playgroud)

和历史似乎没有改变.我正处于我的git知识极限.有人可以帮忙吗?

Rob*_*ley 36

使用BFG Repo-Cleaner,这是一种更简单,更快速的替代方案,git-filter-branch专门用于从Git历史记录中删除文件.

请仔细按照以下步骤操作:https://rtyley.github.io/bfg-repo-cleaner/#usage - 但核心位是这样的:下载BFG jar(需要Java 6或更高版本)并运行此命令:

$ java -jar bfg.jar --delete-folders .git --delete-files .git --no-blob-protection my-repo.git
Run Code Online (Sandbox Code Playgroud)

将扫描整个存储库历史记录,并.git删除任何名为的文件或文件夹.我已经对包含一个文件夹的特殊构造的测试仓库进行了测试.git,它运行得很好.

感谢Michel Jouvin 提醒我,名为'.git'的文件在Git中也是非法的(并且Git报告的错误信息并没有立即显示它们文件,而不是文件夹),我已经更新了这个答案反映这一点.

完全披露:我是BFG Repo-Cleaner的作者.


Gui*_*ont 1

您是否尝试过使用以下命令?

git filter-branch --tree-filter 'rm -Rf .git' HEAD
Run Code Online (Sandbox Code Playgroud)

git filter-branch 文档表明这会比 慢--index-filter,但它可以完成这项工作。