Completely remove files from Git repo and remote on GitHub

I accidentally added a folder of images and committed. Then, I made one more commit.
Then I removed those files using git rm -f ./images and committed again.

Right now, I have made a lot more commits in that branch (master). In my HEAD, I don’t have that ./static/images folder.

  • What is the 'standard' content of .gitignore for an iPhone (Xcode) project?
  • How to grep the git diff?
  • How to configure git to include my name in commits
  • (Git) How can I set parameters using alias
  • Cherry-pick with ignore whitespace / line-endings
  • Order git diff --name-only list by commit/merge date
  • Due to this, my repo size has increased a lot. How can I remove those blobs completely? And I want to remove it from my remote GitHub repo too.

  • How to recover corrupt git repository?
  • When considering git vs svn for work projects, is Mercurial best of both worlds?
  • Is it possible to reject a commit on Github if the commit isn't formatted correctly?
  • How to clone all remote branches in Git?
  • How can I use Git with multiple remote repositories?
  • How do I implement sparse checkout in JGit?
  • 3 Solutions collect form web for “Completely remove files from Git repo and remote on GitHub”

    This is what you’re looking for: ignoring doesn’t remove a file. I suggest you read that page, but here’s the specific command to use:

    git filter-branch --index-filter \
    'git rm -r --cached --ignore-unmatch <file/dir>' HEAD
    

    Also, to remove all the deleted files from caches git creates, use:

    rm -rf .git/refs/original/ && \
    git reflog expire --all && \
    git gc --aggressive --prune
    

    You can find more info about the last command, as well as a script that does everything you want in one single action, here: git: forever remove files or folders from history.

    Another links with lots of explanation: Remove sensitive data.

    [Edit] Also, see this StackOverflow question: Remove sensitive files and their commits from Git history.

    (Commands copied from natacado‘s answer in the question linked above.) If you have already removed the files from the working copy, the following should work. Find out the hash for the commit that added the unwanted files. Then do:

    git filter-branch --index-filter \
    'git update-index --remove filename' <introduction-revision-sha1>..HEAD
    git push --force --verbose --dry-run
    git push --force
    

    You could just rebase your whole branch and remove both the commit that added the images and the commit that removed them.

    git rebase -i master
    

    You will be presented with a list of commits. Delete the lines that have the rogue commits you want to remove. (“dd” deletes a line in vim, the default editor. Then save with ZZ)

    The dangling commits will then be cleaned up in the course of natural git garbage collection, a process you can force with the command given in Darhuuk’s answer.

    Edit: This will work even if you have pushed to a remote repository, but you’ll have to push with –force. (The same applies to the git filter-branch solution).

    Note that this will be very annoying to anyone who has pulled from your branch. They should consult “recovering from upstream rebase”.


    Presumably your original accidental addition of images is part of a commit you wish to keep. In this case, you need to edit the commit during the rebase to split out the parts you wish to keep. You can do this by replacing “pick” in the “rebase -i” list for that commit with “e” (for edit). The rebase process will stop here and you can split the commit with “git commit –amend”.

    I’m essentially repeating the answer combined with this Windows formatting caveat, only so it doesn’t get lost as a comment.

    Note the use of double-quotes rather than single quotes, because it depends on the shell that you’re using how strings are parsed.

    Single Line:

    git filter-branch --index-filter "git rm -r --cached --ignore-unmatch <file/dir>" HEAD
    

    Multiple Lines:

    git filter-branch --index-filter \
        "git rm -r --cached --ignore-unmatch <file/dir>" HEAD
    
    Git Baby is a git and github fan, let's start git clone.