Best way to amend latest commit

I am using Github pages to host a status website with Netlify CMS. Site editors using the CMS will often upload very large images.

I have an actions workflow that runs on push to master. It resizes the images and then uses git-auto-commit-action to commit the result into master.

The issue is that the large images are thus retained in the repo, and as there are many, this is undesirable. I would like to essentially replace the previous commit (containing the unsized images) with a commit containing the resized images, or simply amend the previous commit.

Does anyone know of a specific action that is useful for this?

The git-auto-commit-action has an amend option.

I am not certain and was unable to find a reference, I believe the original commit (and thus the original image) will still exist albeit no longer attached to a branch (I believe they are known as dangling or unreachable commits). This will require contacting GitHub Support to prune these commits. In the past, I’ve contacted Bitbucket to have them prune a repository that had a heavy directory of user-uploaded files accidentally indexed while migrating from SVN to Git.

Interesting. Could this be avoided by doing something like the following?

  • have the CMS push to a different branch (eg feature/image-processor)
  • resize the images in branch feature/image-processor
  • git stash
  • git checkout master
  • git stash pop
  • git commit ...
  • git branch -d feature/image-processor // delete the branch entirely
  • git checkout -b feature/image-processor // recreate the branch off master for runs

Then have a separate workflow that responds to a commit to master to do the rest.

Same thing: The images get pushed to your repository, deleting the branch makes the commits unreferenced. How long unreferenced commits remain depends on when/if GitHub runs garbage collection on repositories, as @mcaskill said the only way to be sure would be to contact support (and I assume you don’t want to do that regularly).

The proper solution would be to have the CMS resize the images before upload (or limit the size of images so users have to).

Huh, you could rewrite the history :confused:

I think github allows rewrite of master.

but I think you should not do so, sorry.

I was looking for this (and somehow missed it). I think it’s a bit charitable to say it has an option. It has documentation on how to perform this task using the action.

Garbage Collection is mentioned elsewhere in the community:

Personally, I’d probably rely on a second unrelated garbage-collecting repository. That way I could delete and recreate the garbage repository at will (forcing garbage collection) instead of having to ask GitHub for help.

1 Like

Once the garbage has been added to GitHub, it’s a bit late.

If you have enough control over the CMS that it doesn’t push to GitHub until you’re happy, then presumably you wouldn’t need to look into things this way.

Same thing: the initial commits become unreferenced.

I agree, this would be the preferred to avoid garbage build up in the primary repository.