To remove a file from the last pushed commit:
|
|
In the editor, change pick to edit, save and close. Then:
|
|
- Note:
--force-with-leaseis safer than--forceas it fails if someone else pushed in the meantime. If it’s a shared branch, warn your team before force-pushing.
But All Traces of the File’s Commit Aren’t Gone
Indeed, the file’s blob object still exists in .git/objects. Git stores content as objects, and even after rewriting history, the old objects remain as dangling/unreachable objects until garbage collected.
To purge it (important if the file contained secrets):
|
|
This removes unreachable objects from the local repository. But if you already pushed the original commit, the object may still exist on the remote (GitHub retains dangling objects for a while). Therefore, for secrets, you should rotate them regardless.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Credit: Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.