Nik Nik - 22 days ago 6x
Git Question

How to remove files from a pushed Git commit

While working on some files, I have created a backup file to each one, but I forgot to erase them before pushing and rebasing. I have committed multiple times since but haven't pushed it yet.

Here were my steps:

git add -A
git commit -m <message>

git checkout master
git pull upstream master
git push
git checkout "branch"
git rebase master

My current branch doesn't have the backup files anymore. Would it be enough to just push and rebase master with my updated branch?


What you could do is to do a soft reset, delete the files, then commit and push.

  1. Do a git log to find the hash of the commit immediately before you added the files
  2. Do a soft reset git reset --soft <hash> (inserting the hash of the commit)
  3. Delete any unwanted files

  4. Stage and commit all files.

This will however squash all of the commits that you have made to your branch to a single commit. If this is acceptable to you, then it would be all that you would need. If you would want to preserve all of your commit history, then you could create a new branch before you added the files, then cherry pick the commit that you added the files, but delete the unwanted files before you make the commit, and then commit just the files you wanted to update. After you do this, you can cherry pick any other commits to that branch as well then push.