1

I would like to modify the current git commit and remove its previous form. In other words the "old" current commit has to disapper and it is replaced by the "new" one with the same commit message.

At the moment I am doing:

git add .
git commit --amend --no-edit
git push --force

From the web interface (gitlab) or using git log, I can see only the "new" current commit; but if I search for the "old" current commit hash (web interface or in .git) I am still able to see the "old" commit content.

Is there a way to completely remove the old "current" commit?

Thanks

Update

Following some advice I have tried:

git add .
git commit --amend --no-edit
git stash clear
git fsck --unreachable --no-reflogs
git reflog expire --expire-unreachable=now --all
git gc --prune=now
git repack
git push --force

while locally I can see that the commits are not present anymore (git branch -a --contains <commit_id>), the size of the repository on gitlab continue to increase, even if I triggered housekeeping.

yellowhat
  • 429
  • 6
  • 17

1 Answers1

3

Is there a way to completely remove the old "current" commit

Not really. A commit to which no name (e.g. branch or tag) points, and is not reachable from any commit to which a name points, will eventually be cleaned up; and you can force that to happen prematurely on your machine. But on GitLab it would require human intervention by the owners of the site to make that happen ahead of schedule. (Though you might be able to make it happen through housekeeping; see How can I force GitLab to carry out a garbage collection (and permanently remove a file))

So at this moment the original ("old" current commit) is slated for destruction, but it has not yet been destroyed. That's not a bug, it's a feature.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the reply. How do I remove it at least locally? I have tried `git gc; git repack` but I can still find the objects in `.git`. – yellowhat Jan 03 '22 at 19:24
  • https://stackoverflow.com/questions/1904860/how-to-remove-unreferenced-blobs-from-my-git-repository – matt Jan 03 '22 at 19:46
  • 1
    The commit is kept because it appears in the reflog. You have to gc telling git to ignore the reflog https://stackoverflow.com/a/14728706/717372 But most of the time, you don't care about an amended commit (and so you don't bother doing a gc) except if you have committed some security information. And if you have pushed it, you should consider it potentially divulgated and absolutely change it... – Philippe Jan 03 '22 at 19:53
  • I have tried some of your advice (see the updated post) but the remote (gitlab) does not change. – yellowhat Jan 04 '22 at 19:58
  • Yeah, I'm no GitLab expert, that's why I said "you might...". :( – matt Jan 04 '22 at 21:41