Git: Remove committed file after push
Solution 1
update: added safer method
preferred method:
check out the previous (unchanged) state of your file; notice the double dash
git checkout HEAD^ -- /path/to/file
commit it:
git commit -am "revert changes on this file, not finished with it yet"
push it, no force needed:
git push
get back to your unfinished work, again do (3 times arrow up):
git checkout HEAD^ -- /path/to/file
effectively 'uncommitting':
To modify the last commit of the repository HEAD, obfuscating your accidentally pushed work, while potentially running into a conflict with your colleague who may have pulled it already, and who will grow grey hair and lose lots of time trying to reconcile his local branch head with the central one:
To remove file change from last commit:
to revert the file to the state before the last commit, do:
git checkout HEAD^ /path/to/file
to update the last commit with the reverted file, do:
git commit --amend
to push the updated commit to the repo, do:
git push -f
Really, consider using the preferred method mentioned before.
Solution 2
If you want to remove the file from the remote repo, first remove it from your project with --cache option and then push it:
git rm --cached /path/to/file
git commit -am "Remove file"
git push
(This works even if the file was added to the remote repo some commits ago) Remember to add to .gitignore the file extensions that you don't want to push.
Solution 3
You can revert only one file to a specified revision.
First you can check on which commits the file was changed.
git log path/to/file.txt
Then you can checkout the file with the revision number.
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /path/to/file.txt
After that you can commit and push it again.
Solution 4
You can use the following workflow:
git reset --soft HEAD~1
git reset HEAD /path/to/file
Solution 5
Reset the file in a correct state, commit, and push again.
If you're sure nobody else has fetched your changes yet, you can use --amend
when committing, to modify your previous commit (i.e. rewrite history), and then push. I think you'll have to use the -f
option when pushing, to force the push, though.
Related videos on Youtube
kmaci
Updated on March 22, 2022Comments
-
kmaci about 2 years
Is there a possibility to revert a committed file in Git? I've pushed a commit to GitHub and then I realized that there's a file which I didn't want to be pushed (I haven't finished the changes).
-
rdrmntn over 10 yearsDo you want to remove the file completly from GitHub after a push? Otherwise you just: git rm <file>, git push.
-
René Höhle over 10 years
-
Admin over 10 yearsPossible duplicate of How can I remove a commit on github? and How to undo the last Git commit?.
-
-
richardm over 6 yearsUnless you're the only developer on a project, you really shouldn't use git push -f. It creates way more problems than it's worth. Just remove the file then do a new commit.
-
starscream_disco_party over 5 yearsIs this doable with commits further back in history? E.g. Could I do this with
git checkout HEAD~2 /path/to/file
? Edit: Looks like what I wanted in my case was simplygit rm /path/to/file
-
Ken about 5 yearsThis does remove the files from this commit forward, but the files are still visible within the older commits.
-
dev_ios999 over 3 yearswow thank you, i accidentally committed and pushed something and your answer was a life saver :)
-
whatapalaver about 3 yearsUsing zsh on my mac and I had to change the command to
git checkout HEAD~ /path/to/file
to avoid having to escape. -
Richard Rebeco almost 3 yearsI like because it removes files only for git also when .gitignore doesn't works fine. I want to say something: if you need remove a lot of files, you can use the next commands: git rm -r --cache /path/to/file As you can see i added the "-r" optional command to make the work recursively. Thanks a lot @micoru <3
-
General Grievance over 2 yearsThis should have been a comment or an edit to the referenced answer.
-
J Olson over 2 yearsUse
git rm --cached /path/to/file
then add, commit and push. Just like above answer, but you need to use--cached
not--cache
.