Skip to content

Instantly share code, notes, and snippets.

@bhpayne
Created June 14, 2021 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhpayne/8f6b4f0cf8904adb17f1ac8d6961134b to your computer and use it in GitHub Desktop.
Save bhpayne/8f6b4f0cf8904adb17f1ac8d6961134b to your computer and use it in GitHub Desktop.
fake git history for testing file deletion

Following command edits the bare repo (rather than a clone of the repo)

cd bare_git_repo
git filter-branch --prune-empty -d /Users/username/software/git_test/git_backup --index-filter "git rm --cached -f --ignore-unmatch a_large_binary" --tag-name-filter cat -- --all

Then run the following on the bare repo

git update-ref -d refs/original/refs/heads/master
git reflog expire --expire=now --all
git gc --prune=now

Now that the bare repo is cleaned, users should discard old clone and get a new clone

cd cloned_git_repo
rm -rf bare_git_repo/
git clone file:///Users/username/software/git_test/bare_git_repo/
cd bare_git_repo/

Verify in the cloned version the file is actually gone:

git log --graph --decorate --pretty=oneline --abbrev-commit --all --name-status
#!/usr/bin/env bash
# chmod u+x make_and_delete_big_file.sh
# rm -rf bare_git_repo/ cloned_git_repo/
set -eux
STARTING_DIRECTORY=`pwd`
mkdir bare_git_repo
cd bare_git_repo
GIT_BARE_REPO_DIRECTORY=`pwd`
git init --bare
cd ${STARTING_DIRECTORY}
mkdir cloned_git_repo
cd cloned_git_repo
GIT_CLONED_REPO_DIRECTORY=`pwd`
git clone ${GIT_BARE_REPO_DIRECTORY}
cd bare_git_repo
cat << EOF > README.md
This is the first file
commited to the repo
EOF
git add README.md
git commit -m "first file"
git push
cat << EOF > a_small_file.dat
Second commited file
in the repo
EOF
git add a_small_file.dat
git commit -m "second file"
git push
cat << EOF > a_large_binary
This file should be removed
from the repo
EOF
git add a_large_binary
git commit -m "a binary"
git push
cat << EOF > another_small_file.dat
This file was added
after the binary
EOF
git add another_small_file.dat
git commit -m "post-binary file"
git push
cat << EOF > yet_another.dat
latest file
in repo
EOF
git add yet_another.dat
git commit -m "latest"
git push
@bhpayne
Copy link
Author

bhpayne commented Jun 14, 2021

To find large files in the cloned repo, the command

git verify-pack -v .git/objects/pack/*.idx | grep blob | cut -c1-40,48- | cut -d' ' -f1,3 | sort -n -r --key 2

is from https://gist.github.com/rtyley/4202730

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment