Rebasing and squashing commits will actually make the .git folder BIGGER, at least until the replaced commits are prune
-d. This is because git keeps the commits it "replaces" around after rebasing. See output from reflog
.
To keep the history intact, but the .git directory small, you could try using a shallow clone to keep copies of the repo small. This may involve re-cloning (with clone --depth=1
) into a different directory.
There's a cheaper way to rebase, and it doesn't involve rebase
at all. Restart the git history by periodically doing this:
git reset --soft "$(git rev-list --max-parents=0 HEAD |head -1)"
git commit -m 'First commit message'
Your original solution can be done with a specially-crafed EDITOR
command:
#!/bin/bash
NUM_LINES=$(wc -l < "$1")
KEEP=1000
if [[ $NUM_LINES > $KEEP ]]; then
# [Line numbers shown] Transform:
# 0001 pick abcdef
# 0002 pick abcdef
# .... pick ...
# 1000 pick abcdef
# 1001 pick abcdef
#
# To:
# 0001 pick abcdef
# 0002 f abcdef
# .... f ...
# 1000 f abcdef
# 1001 pick abcdef
#
# Importantly, the rebase instruction file cannot begin with a `fixup` instruction.
exec sed -e "2,$(( NUM_LINES - KEEP ))"'s/^\(pick\|p\) /f/' "$@"
fi
Save it as squash-first-thousand.sh
and reference it when you rebase:
EDITOR=/path/to/squash-first-thousand.sh \
git rebase -i "$(git rev-list --max-parents=0 HEAD |head -1)"
You can also use export EDITOR=...