If we have a git repo with a lot of commits and we do not want them, we can remove all commits from before a date. Source

  1. Make a backup. We may run into issues.
REPO="newsboat"
cd "$REPO"
find . -type f -not -name files.sha256 -exec sha256sum "{}" >> files.sha256 \;
cd ..
tar --acls --xattrs -cpf "$REPO.$( date +'%Y.%m.%d').tar" "$REPO"
sha256sum "$REPO.$( date +'%Y.%m.%d').tar" > "$REPO.$( date +'%Y.%m.%d').tar.sha256"
  1. List all commits.
PAGER= git log --pretty=oneline --abbrev-commit
---
40c6a94 host - 2021-04-16_22-15
c23b1db host - 2021-04-13_22-10
dfc352d host - 2021-04-12_22-24
57d2576 host - 2021-04-11_22-16
1557426 host - 2021-04-10_22-30
2402520 host - 2021-04-09_22-12
0b4191d host - 2021-04-08_22-37
a7af341 host - 2021-04-07_22-13
dc342c9 host - 2021-04-05_22-05
fda2456 host - 2021-04-04_22-34
d40dc07 host - 2021-03-23_21-28
5ce3c1b host - 2021-03-21_22-34
[...]

We want to keep all commits up to 5ce3c1b host - 2021-03-21_22-34 deleting the older ones.

  1. Create a new branch from the commit 5ce3c1b. This branch will be the one to become master.
git checkout --orphan temp 5ce3c1b
---
Switched to a new branch 'temp'
  1. Create a new commit.
git commit -m "new root commit"
---
[temp (root-commit) b2d2aee] new root commit
 4 files changed, 9811 insertions(+)
 create mode 100644 file.txt
  1. Rebase the part of history from the commit you want to master on the temp branch.
git rebase --onto temp 5ce3c1b master
---
Successfully rebased and updated refs/heads/master.
  1. Delete the temp branch.
git branch -D temp
---
Deleted branch temp (was b2d2aee).
  1. Make sure there are no changes. The output should be empty.
PAGER= git diff origin/master
  1. Forcefully push the changes to the remote server.
git push -f
  1. (Optional) Delete all the objects w/o references.
git prune --progress
  1. (Optional) Aggressively collect garbage; may take a lot of time on large repos.
git gc --aggressive
---
Enumerating objects: 660, done.
Counting objects: 100% (660/660), done.
Delta compression using up to 12 threads
Compressing objects: 100% (657/657), done.
Writing objects: 100% (660/660), done.
Total 660 (delta 416), reused 2 (delta 0), pack-reused 0

And that’s it, we have only the wanted commits.

PAGER= git log --pretty=oneline --abbrev-commit
---
fe779e5 host - 2021-04-16_22-15
b2acdfa host - 2021-04-13_22-10
4d39613 host - 2021-04-12_22-24
a01cb00 host - 2021-04-11_22-16
bc7182a host - 2021-04-10_22-30
926adf2 host - 2021-04-09_22-12
00daf33 host - 2021-04-08_22-37
9189e8b host - 2021-04-07_22-13
ababf20 host - 2021-04-05_22-05
f1d55af host - 2021-04-04_22-34
7f7289a host - 2021-03-23_21-28
b2d2aee new root commit