Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. # Completely remove a file from a git repository history
  2. #
  3. # Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
  4. # GPL licensed (see end of file) * Use at your own risk!
  5. #
  6. # Usage:
  7. # git-forget-blob file_to_forget
  8. #
  9. # Notes:
  10. # It rewrites history, therefore will change commit references
  11. function git-forget-blob()
  12. {
  13. local BLOBS=( $( git verify-pack -v .git/objects/pack/*.idx | grep blob | awk '{ print $1 }' ) )
  14. for ref in ${BLOBS[@]}; do
  15. local FILE="$( git rev-list --objects --all | grep $ref | awk '{ print $2 }' )"
  16. [[ "$FILE" == "$1" ]] && break
  17. unset FILE
  18. done
  19. [[ "$FILE" == "" ]] && { echo "$1 not found in repo history" && return; }
  20.  
  21. git filter-branch --index-filter "git rm --cached --ignore-unmatch $FILE"
  22. rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/
  23. git for-each-ref --format="%(refname)" refs/original/ | xargs -n1 --no-run-if-empty git update-ref -d
  24. git reflog expire --expire-unreachable=now --all
  25. git repack -A -d
  26. git prune
  27. }
  28. # License
  29. #
  30. # This script is free software; you can redistribute it and/or modify it
  31. # under the terms of the GNU General Public License as published by
  32. # the Free Software Foundation; either version 2 of the License, or
  33. # (at your option) any later version.
  34. #
  35. # This script is distributed in the hope that it will be useful,
  36. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. # GNU General Public License for more details.
  39. #
  40. # You should have received a copy of the GNU General Public License
  41. # along with this script; if not, write to the
  42. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  43. # Boston, MA 02111-1307 USA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement