Example
So let's say I have a local git repo with 10 commits, having SHA digests 0-9 so my git log looks like this
9 (HEAD -> master)
8
7
6
5
4
3
2
1
0 <- initial commit
and I decide that commits 5-9 are garbage and I would like to permanently delete all record of them from the repositoy and the disk space they introduced. Baiscally, I want the state of my repo to be the same as it was when commit 4 was made, and have it be like 5-9 never even happened.
I know that git reset --hard 4 will make my repo appear to have been rewinded to commit 4, but from what I understand, that merely changes the commit master points to from 9 to 4 but does not actually delete anything. All the data is still there, and is recoverable if you know the SHA of commit 9.
I am also aware of git filter-branch but that only removes files from the history, not commits.
Ive tried doing:
git reset --hard 4
git gc --prune=now
but after doing this, the disk space usage of my .git directory is the same or bigger, and I can still recover the history with git checkout 9. Why does git gc --prune=now not prune commits 5-9? Do I need to expire my reflog?
More Generally:
If I have a complex repo with many branches, tags, commits, merges and divergant history, How can I permanently and automatically remove all commits, along with the changes they introduce, and the disk space they consume, that occured after a certain time. Effectively rewinding the entire repo to that time and permanently destroying all activity that occurred after that date.
git resetdoes not delete content. its simply make change your HEAD to point to the new SHA-1 you asked for.How can i delete content?
Let me correct you.
Once you do a git filter branch its updating the content and creating a new commit.
So where is the old commit?
The old commit it still in your repository. Its becoming a dangling object, which means that there is some content which is not reachable from any branch.
First of all read this answer to understand what is HEAD.
Now you have to use
git filter-branchof BFG and only than execute thegit gc.You have several option to achieve it. Here is a simple one:
It does not remove the commits since reset only change the HEAD and not the content of the repository.