So, I am trying to rename a folder in my repository using git filter-branch --index-filter, but I always end up with a \"fatal: bad source\" error.
The
git mv is not really appropriate in an --index-filter clause. Since --index-filter does not check out each commit it's rewriting into the working directory, and git mv operates on the working directory (in addition to the index), using git mv in --index-filter will not accomplish what is expected. Use a --tree-filter instead. (It might be possible to accomplish this still with --index-filter by using git update-index instead, but I don't have that available off the top of my head).
As twalberg points out in his answer, git mv doesn't just access the index, it also access the disk. That's probably the reason why it doesn't work.
I didn't want to use the slow --tree-filter so I tried to change the sample from the git filter-branch man page that shows how to move the complete repository into a subfolder.
The result is this - and it actually works ;-)
git filter-branch --index-filter '
git ls-files -s | \
sed "s-\(\t\"*\)my_file.txt-\1your_file.txt-" | \
GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && \
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
' HEAD