How to permanently delete a Git important files or folders (including history) forced

Sometimes accidentally uploaded some sensitive files (such as passwords), or do not want to upload a file (no time or forgot added .gitignore Lane),

And upload the file has been particularly large, this will lead to other people clone your zip code or download the package must also be updated when or download these useless files,

Therefore, we need a way to permanently delete these files (including historical records of the file).

First, you can refer to the help of github:

https://help.github.com/articles/remove-sensitive-data

Step one:  remove files from your library

For example the following Windows (Linux is similar), open the project's Git Bash, use the command: 

 
$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch path-to-your-remove-file' --prune-empty --tag-name-filter cat -- --all

Which, path-to-your-remove-file that you want to delete a relative path to the file (relative to the git repository with the directory), you can replace the file you want to delete. Note that in mind, here's a file or folder, You can not begin with '/', otherwise the file or folder will be considered from the beginning of the installation directory git.

If you want to delete the target is not a file, but a folder, then the `git rm --cached 'command -r command later to add, delete represent recursive (sub) folders and files in the folder, similar to` rm -rf` command.

Also, if you want to delete a lot of files, it can be written into a batch execution .sh file, if the file or the path, there are Chinese, because the Chinese language MinGW or CygWin path settings too much trouble, you can use the wildcard asterisk, for example: sound / music _ *. mp3, so put the sound to mp3 files in the directory that begin music_ are deleted.

For example this way, create a bash script file, del-music-mp3.sh:


$  git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch projects/Moon.mp3' --prune-empty --tag-name-filter cat -- --all git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch sound/Music_*.mp3' --prune-empty --tag-name-filter cat -- --all

 

 If you see something like this, it shows the success of the deleted:
 
Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
# Ref 'refs/heads/master' was rewritten

If the xxxxx unchanged, there are no instructions repo find the file, check the path and file name are correct.

Note: add that if you want to and they will not re-upload the file or folder, the file or folder to add to .gitignore file, then push your repo. 

Step two: push repo our revised

Forced to push to cover your repo, the command is as follows:

$ git push origin master -f

 

This process is actually re-upload our repo, time-consuming, though with deleted rebuild a repo somewhat similar, but the benefits are to retain the original update records, so it is somewhat different. If you really do not care about these update records , reconstruction may be deleted, both not too bad, and perhaps some of the latter more intuitive.

The results similar to the following:

Counting objects: 4669, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4352/4352), done.
Writing objects: 100% (4666/4666), 35.16 MiB | 51 KiB/s, done.
Total 4666 (delta 1361), reused 0 (delta 0)
To https://github.com/defunkt/github-gem.git
 + beb839d...81f21f3 master -> master (forced update)

 

In order to fight the tag from the version you also delete the specified file or folder, you can use this command to force push your Git tags:
 
$ git push origin master --force --tags

 

Step three:  clean up and reclaim space

Although the above we have deleted files, but we still retained the repo inside these objects, waiting for garbage collection (GC), so we use the command to completely remove it and reclaim space.

Command is as follows:

$ rm -rf .git/refs/original/

$ git reflog expire --expire=now --all

$ git gc --prune=now

Counting objects: 2437, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (1378/1378), done.
# Writing objects: 100% (2437/2437), done.
# Total 2437 (delta 1461), reused 1802 (delta 1048)

$ git gc --aggressive --prune=now

Counting objects: 2437, done.
# Delta compression using up to 4 threads.
# Compressing objects: 100% (2426/2426), done.
# Writing objects: 100% (2437/2437), done.
# Total 2437 (delta 1483), reused 0 (delta 0)

Now you look at your .git directory the file size is not smaller.

Guess you like

Origin www.cnblogs.com/mafeng/p/10959874.html