how to remove a file from a git commit that has been pushed

18,608

Solution 1

Warning, this will overwrite the changes remotely. If anyone has taken down the current commit and worked on top of it, bad things could happen.

# Reset last commit, but keep changes staged
$ git reset --soft HEAD^1

# Unstage unwanted file(s) and recommit
$ git reset HEAD path/to/file
$ git commit

# Push the new commit with force to overwrite changes
$ git push origin -f

Solution 2

You can just remove these files locally and push a new commit.

If you like to excude these files in future you can add it to .gitignore file

Share:
18,608
Admin
Author by

Admin

Updated on June 18, 2022

Comments

  • Admin
    Admin about 2 years

    I mistakenly used git commit -a and added more files than I wanted to in a commit. There was another modified file that I wanted to add as a separate commit included in this commit. How do I go back ( without losing the modifications to the files ) and then commit them all again separately.

    I've also pushed this to a remote origin ( though I know for a fact that no one has pulled anything since I pushed).

    Many Thanks!