How to remove a file from the previous commit?

Sometimes, you would want to remove a specific file or part of the code from your last commit.

Sometimes, you would want to remove a specific file or part of the code from your last commit. To do it, do the following:

git checkout HEAD^ myfile # this revert the file to the last commit.
git add myfile
git commit --amend --no-edit

In case you don't have a history or said: "It was a new file." You need to remove it from the account altogether.

git rm --cached myfile
git commit --amend --no-edit

This is particularly useful when you have an open patch and have committed an unnecessary file and need to force push to update the patch on a remote. The --no-edit option is used to keep the existing commit message.

Originally Posted On: Github (k88hudson)

Subscribe to GIT.WTF!?!

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe