BFG-clean repository

If you’ve had the bad habit of checking in binary data, this is your chance to clean it up. If so, then run these lines. Tweak the bfg command to your taste.

cd git-repo

# Optional. Will likely make you able to remove more files. You might want to remove remotes as well.
git tag -d $(git tag -l)

cd ..
java -jar bfg-1.13.0.jar --strip-blobs-bigger-than 5M --delete-files "*.{class, jar, png, svg, ico, zip}" git-repo
cd git-repo
git reflog expire --expire=now --all && git gc --prune=now --aggressive

Move the repository to a subfolder

Replace subfolder below with the real subfolder name

git filter-branch --index-filter 'git ls-files -s | sed "s-\t\"*-&subfolder/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD

If the above commands fails, you can force it to work by changing the suffix from ..."$GIT_INDEX_FILE"' HEAD to …"$GIT_INDEX_FILE"'; /bin/true HEAD which seems to work (TL;DR). See this

Merge repository into an existing repository

If you want to migrate tags you need to replace git fetch old-repo with git fetch old-repo --tags

git remote add old-repo ../git-repo
git fetch old-repo
git merge --allow-unrelated-histories old-repo/master
git remote remove old-repo

And you are done!

Other stuff you also can do

Rename subdirectory

Not fully tested this one, so be careful with it

git filter-branch --index-filter \
        'git ls-files -s | sed "s/wrongname/correctname/" |
                GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                        git update-index --index-info &&
         mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD