Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

gitlab

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!

like image 394
ianus Avatar asked Aug 25 '15 19:08

ianus


People also ask

Can you remove a file from a commit git?

If this is your last commit and you want to completely delete the file from your local and the remote repository, you can: remove the file git rm <file> commit with amend flag: git commit --amend.

Can you remove a push in git?

In order to delete a remote Git tag, use the “git push” command with the “–delete” option and specify the tag name.


2 Answers

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
like image 109
Sam Avatar answered Sep 30 '22 12:09

Sam


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

like image 31
RafalskyCom Avatar answered Sep 30 '22 13:09

RafalskyCom