Công nghệ Mẹo vặt

Cách giảm dung lượng thư mục .git

Table of Content

Cách giảm dung lượng thư mục .git

How to shrink the .git folder

Nhận dạng

Do mình viết script để tự động backup lên github, giờ mới phát hiện ra là mỗi lần commit, push vậy là nó sẽ tạo 1 bản sao và làm thư mục ngày phình to ra

image

image

Giải quyết

If you want to clean all previous commit and thin up your repo.
Warning: this operation will make you lost all previous commit

## This script is used to clean all git commit
if [[ "$1" = 'all' ]];then
    echo "Clean all git commit"
    git checkout --orphan latest_branch
    git add -A
    git commit -am "Delete all previous commit"
    git branch -D master
    git branch -m master
fi

echo "Cleanup refs and logs"
rm -Rf .git/refs/original
rm -Rf .git/logs/

echo "Cleanup unnecessary files"
git gc --aggressive --prune=now

echo "Prune all unreachable objects"
git prune --expire now

#git push -f origin master

https://github.com/ahuigo/a/blob/master/tool/gitclean.sh

https://github.com/18F/C2/issues/439#issuecomment-427698726

Kết quả

Code tổng hợp của mình dùng để dọn server

#!/bin/bash

# Adapted from 71529-ubucleaner.sh - http://www.opendesktop.org/CONTENT/content-files/71529-ubucleaner.sh

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
YELLOW="\033[1;33m"; RED="\033[0;31m"; ENDCOLOR="\033[0m"

if [ $USER != root ]; then
  echo -e $RED"Error: must be root! Exiting..."$ENDCOLOR
  exit 0
fi

echo -e $YELLOW"Cleaning apt ..."$ENDCOLOR
aptitude clean
apt-get autoremove
apt-get autoclean

echo -e $YELLOW"Those packages were uninstalled without --purge:"$ENDCOLOR
echo $OLDCONF
#apt-get purge "$OLDCONF"  # fixes the error in the original script
for PKGNAME in $OLDCONF ; do  # a better way to handle errors
  echo -e $YELLOW"Purge package $PKGNAME"
  apt-cache show "$PKGNAME"|grep Description: -A3
  apt-get -y purge "$PKGNAME"
done

echo -e $YELLOW"Removing old kernels..."$ENDCOLOR
echo current kernel you are using:
uname -a
aptitude purge $OLDKERNELS

echo -e $YELLOW"Emptying every trashes..."$ENDCOLOR
rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null

echo -e $YELLOW"Script Finished!"$ENDCOLOR



# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done
echo "Done Removes old revisions of snaps"

image

Tham khảo

  1. https://stackoverflow.com/questions/5613345/how-to-shrink-the-git-folder/8483112
  2. https://www.reddit.com/r/git/comments/4xsh26/why_is_my_git_folder_so_big/
  3. https://medium.com/@sangeethkumar.tvm.kpm/cleaning-up-a-git-repo-for-reducing-the-repository-size-d11fa496ba48
  4. https://github.com/18F/C2/issues/439

Leave a Reply

Your email address will not be published. Required fields are marked *