在Claude code、Vibe kanban等vibe coding工具中,普遍使用git worktree功能创建新的工作目录,作为AI agent的工作空间。当原仓库使用git-crypt加密时,会出现无法创建工作目录的情况。下面给出解决方案。
典型现象是,当尝试添加worktree后,出现以下错误信息:
Preparing worktree (new branch 'newtree2')
git-crypt: Error: Unable to open key file - have you unlocked/initialized this repository yet?
error: external filter '"git-crypt" smudge' failed 1
error: external filter '"git-crypt" smudge' failed
fatal: *some/crypted/files*: smudge filter git-crypt failed
甚至这个仓库在此以后进行任何git操作都会出现:
git-crypt: Error: Unable to open key file - have you unlocked/initialized this repository yet?
error: external filter '"git-crypt" clean' failed 1
error: external filter '"git-crypt" clean' failed
fatal: *some/crypted/files*: clean filter git-crypt failed
当前git-crypt官方最新版本为0.8.0,目前依然无法兼容 git worktree 功能。 根据git-crypt的Milestones信息,这个问题预计在0.9.0版本中修复,但是发布时间未定。git-crypt 0.7.0 版本和 0.8.0 版本间隔了3年,这么看2026年年内能不能发布都不好说。 社区作者morganwahl已经提交了修复版本,当前可以编译这个修复版本来解决问题。下面给出编译的详细步骤。
使用的社区仓库:https://github.com/morganwahl/git-crypt.git 提交版本: 2da5e0016e53aba381046063c24c07f1bee3d824,内容是morganwahl在git-crypt 0.6.0版本基础上添加了一个针对git worktree的修复,预计在0.9.0版本中合入git-crypt主仓库。
git命令需要访问github仓库,如果遇到网络问题,可添加代理配置。git clone命令改成这样:
git -c http.proxy="http://your.proxy.ip:7890" -c https.proxy="http://your.proxy.ip:7890" clone https://github.com/morganwahl/git-crypt.git
sudo apt install make git g++ libssl-dev
git clone https://github.com/morganwahl/git-crypt.git
cd git-crypt
git checkout 2da5e0016e53aba381046063c24c07f1bee3d824
make
编译结果: 生成的可执行文件位于 ./git-crypt
Windows环境: 必须使用MSYS2 MinGW x64终端,确保PATH设置正确
在 MSYS2 MinGW x64终端 (不是msys2终端) 中执行:(如果要选择tollchain类型,选择g++或者gcc)
sudo pacman -S --needed base-devel mingw-w64-x86_64-toolchain mingw-w64-x86_64-openssl git make
which g++
git clone https://github.com/morganwahl/git-crypt.git
cd git-crypt
git checkout 2da5e0016e53aba381046063c24c07f1bee3d824
make clean
make CXXFLAGS="-I/mingw64/include -DOPENSSL_API_COMPAT=0x30000000L" LDFLAGS="-static-libstdc++ -static -lcrypto -lcrypt32 -lws2_32 -L/mingw64/lib"
编译结果: 生成的可执行文件位于 ./git-crypt.exe
更新git-crypt后,新clone的仓库可以正常使用worktree和各种git操作,但是旧的本地加密的仓库依然可能存在问题,会发现各种git操作失败,显示上述git-crypt clean错误。 这时可以重新初始化.git文件夹,重新clone一个新仓库,删除旧.git文件夹,复制新仓库的.git文件夹到旧仓库目录下,即可解决。 这个方法应该不是最优雅的解决方案。