Git: Remove committed file after push

GitGithub

Git Problem Overview


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).

Git Solutions


Solution 1 - Git

update: added safer method

preferred method:
  1. check out the previous (unchanged) state of your file; notice the double dash

     git checkout HEAD^ -- /path/to/file
    
  2. commit it:

     git commit -am "revert changes on this file, not finished with it yet"
    
  3. push it, no force needed:

     git push
    
  4. 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:

  1. to revert the file to the state before the last commit, do:

     git checkout HEAD^ /path/to/file
    
  2. to update the last commit with the reverted file, do:

     git commit --amend
    
  3. to push the updated commit to the repo, do:

     git push -f
    

Really, consider using the preferred method mentioned before.

Solution 2 - Git

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 - Git

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 - Git

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.

Solution 5 - Git

You can use the following workflow:

git reset --soft HEAD~1
git reset HEAD /path/to/file

Solution 6 - Git

Use git rm --cached /path/to/file then add, commit and push. Just like above answer, but you need to use --cached not --cache.

Solution 7 - Git

  1. Get the hash code of last commit.

    • git log
  2. Revert the commit

    • git revert <hash_code_from_git_log>
  3. Push the changes

    • git push

check out in the GHR. you might get what ever you need, hope you this is useful

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkmaciView Question on Stackoverflow
Solution 1 - GitxorView Answer on Stackoverflow
Solution 2 - GitmicoruView Answer on Stackoverflow
Solution 3 - GitRené HöhleView Answer on Stackoverflow
Solution 4 - GitJB NizetView Answer on Stackoverflow
Solution 5 - GitDimitris ParaschakisView Answer on Stackoverflow
Solution 6 - GitJ OlsonView Answer on Stackoverflow
Solution 7 - Gitkrish babuView Answer on Stackoverflow