remove file from pushed commit without removing it from tree git

Viewed 92

I wanted to remove a file added by mistake in a commit in Git

I tried this code, but was surprised that it remove the file definitively. who can help me please?

Please who can help me? All my tests have failed.

I did when following this link Remove files from Git commit:

git reset --soft HEAD^ 
git reset --soft HEAD~1
git reset HEAD path/to/unwanted_file
git commit -m 'remove unwanted file'
1 Answers

I would do it like this.

git checkout revision-where-it-was-added
git rm --cached the-file
git commit --amend --no-edit
git cherry-pick HEAD@{1}..the-branch # replay the other revisions in the branch
# if you like this resulting history:
git branch -f the-branch
Related