ianus
ianus

Reputation: 73

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

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!

Upvotes: 7

Views: 24237

Answers (4)

Sam
Sam

Reputation: 20486

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

Upvotes: 13

GolezTrol
GolezTrol

Reputation: 116110

If you pushed it to a separate branch (not the master), you can delete the whole branch from the remote.

After that, you can revert the commit using

git reset --soft HEAD~1

Then you can start committing and pushing everything again.

Upvotes: -1

RafalskyCom
RafalskyCom

Reputation: 21

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

Upvotes: 2

Courtney Faulkner
Courtney Faulkner

Reputation: 2080

I would immediately push the previous commit to the remote, run git reset HEAD~ to put your workspace to a dirty state you were in, and then reprocess your adding, committing, and pushing.

Upvotes: 0

Related Questions