getting rid of file commited and pushed two commits behind HEAD

Viewed 49

I have a situation where two commits past I have reverted changes from another commit, on that revert commit I also committed package.json and package-lock.json, and now I need to get rid of those before I will rebase to master and have my PR approved. How can I achieve that without overnighting currently committed files, I mean without reverting to the commit where I have committed those unwanted files?

Many thanks

1 Answers

The simplest solution is to create a new commit where you are, deleting those files.

git rm package.json package-lock.json
git commit

If having so many commits is a problem, you can use rebase -i to re-order and/or squash commits. But in most cases, the simple solution is fine. You can then rebase your branch to master, and your PR can just include all the commits, including the one where you deleted the unwanted files.

In some projects, the upstream maintainer (the one who approves and merges your PR) will do the work of squashing and/or reordering commits instead of asking you to do it.

Related