Just found an even easier method if you're using GitLab and a code review/merge request step. Found this after I accidentally included an unnecessary file in a commit & push. You can also use this method to make edits and recommit them to your branch before merging.
Our process: After coding, we commit, fetch from upstream, rebase, push to GitLab. This creates a branch on GitLab, we then create a merge request for that branch. This is to ensure all code gets code reviewed by a peer before going in the main branch. In this case it was an extremely useful step as my peer review colleague spotted my gaff.
Note: This is only an option BEFORE a merge request is accepted.
To remove a file from a commit after pushing to GitLab and BEFORE merging:
- Open the GitLab merge request
- Select the 'Changes' tab
- Find the file that is unwanted in the commit
- Click the 'View file' button for the file
- Click the 'Delete' button
- Enter the commit information and commit the change
Your merge request has now been fixed and the unwanted file removed from your commit. You can now merge (if the rest pass code review).
You can also edit and recommit files this way. It's an easy way to make quick fixes without needing to open Eclipse/IntelliJ (whatever other tool you use) and checking out the branch.
This just saved me hours of pain avoiding dealing with removing a file from a pushed branch in Eclipse.