Rewrite history

change initial commit

git rebase -i --root

reformat code in commits

git rebase

git rebase main -x 'make clang-reformat && git commit --amend'

git-filter-repo

wget https://raw.githubusercontent.com/newren/git-filter-repo/main/contrib/filter-repo-demos/lint-history
chmod +x lint-history

Apply patch from https://github.com/newren/git-filter-repo/issues/552

./lint-history \
  --refs main..feeat-clang2 \
  --relevant 'return filename.endswith(b".c") or filename.endswith(b".h")' \
  clang-format-16 -i

reword commits

Automatically reword all rebased commits

Following will automatically execute default editor for every commit:

git rebase origin/master -x 'git commit --amend'

Remove files from history (rewriting)

Iterate through history and remove all files mathcing pattern:

git filter-branch --tree-filter 'rm -f m* z* x* w* s* r*' --prune-empty  HEAD
sudo apt install git-filter-repo

Remove workspace dir:

git filter-repo --path workspace --invert-paths --force

Move from subdir to root

git filter-repo --path-rename 'MYDIR/:'

Move from root to subdir

git filter-repo --path-rename ':app/'

apply dos2unix

git filter-branch --tree-filter 'git ls-files -z | xargs -0 dos2unix' -- --all

clean backups refs

filter-branch makes backup by creating refs/oribinal/*. To clean all backups refs from rewriting history tools:

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git for-each-ref --format="%(refname)" refs/replace/ | xargs -n 1 git update-ref -d