Guest User

squash-by-month.sh

a guest
Oct 29th, 2025
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.69 KB | Source Code | 0 0
  1. #!/bin/bash
  2. DATE=""
  3.  
  4. # Get the last commit of each month (not the first)
  5. git log --date=raw --pretty='format:%cd %H' | while read -r time tz hash; do
  6.   CDATE=$(gdate -d @"$time" '+%Y-%m')
  7.   if [ "$DATE" != "$CDATE" ]; then
  8.     echo "$hash" >> ~/Desktop/tmp.txt
  9.     DATE=$CDATE
  10.   fi
  11. done
  12.  
  13. # Rewrite history to squash commits monthly
  14. git filter-branch -f --commit-filter '
  15.  # Check if the current commit hash is in the tmp file
  16.  if grep -q "$GIT_COMMIT" ~/Desktop/tmp.txt; then
  17.    git commit-tree "$@"
  18.  else
  19.    # Skip the commit but merge its changes into the next kept commit
  20.    skip_commit "$@"
  21.  fi
  22. ' HEAD
  23.  
  24. # Optional: Force push (use with caution)
  25. # git push --force-with-lease
  26.  
Advertisement
Add Comment
Please, Sign In to add comment