Version Control
Git
-
shorten git history
# BURN git to the ground, keep only latest commit, shrink size: # https://gist.github.com/stucka/2973d0702f05003c5061e57b3df29f70 # adapted from https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository git checkout --orphan newBranch git add -A # Add all files and commit them git commit git branch -D master # Deletes the master branch git branch -m master # Rename the current branch to master git gc --aggressive --prune=all # remove the old files git push -u -f origin master # Force push master branch to github git gc --aggressive --prune=all # remove the old files
# REMOVE all but LAST N COMMITS from git history to save space # https://stackoverflow.com/questions/72995274/how-to-remove-all-but-last-n-commits-from-git-history-to-save-space git switch --detach <new-repo-root-commit-id> git reset --soft <old-repo-root-commit-id> git commit --amend --reuse-message=<new-repo-root-commit-id> git rebase <new-repo-root-commit-id> main --onto @
- the process is:
- the branch name you wish to rewrite. (this answer will assume it is called
main
.) - the root commit of your branch. Get it with:
git log main --reverse
(this answer will assume it is called<old-repo-root-commit-id>
.) - the oldest commit ID you want to keep, e.g. your forth commit from the top, which will become your new repo root commit. (this answer will assume it is called
<new-repo-root-commit-id>
.) - your
git status
should be clean before you start. If it isn’t, consider committing (or undoing) your most recent changes.
- the branch name you wish to rewrite. (this answer will assume it is called
# TRUNCATING git history # https://passingcuriosity.com/2017/truncating-git-history/ git checkout --orphan temp e41d7f633c45c46bd42e97cecf93204191d9e4c9 git commit -m "Truncate history" git rebase --onto temp e41d7f633c45c46bd42e97cecf93204191d9e4c9 master
- the process is:
- make a new branch (I’ll call it
temp
below); and - use
rebase
to replay every subsequent commit on top of the new branch.
- make a new branch (I’ll call it
- the process is:
-
find size of git repository
# https://stackoverflow.com/questions/8185276/find-size-of-git-repository - git count-objects -vH