Ultimate Git Scripts Collection

Auto Commit with Timestamp

#!/bin/bash
FILES_CHANGED=$(git status --porcelain | wc -l)
git add .
git commit -m "[AUTO] Update $(date +'%Y-%m-%d %H:%M:%S') - $FILES_CHANGED files changed"
git push

Create Feature Branch from Develop

#!/bin/bash
BRANCH=$1
git checkout develop
git pull origin develop
git checkout -b feature/$BRANCH
echo "Feature branch feature/$BRANCH created from develop"

Delete Local Merged Branches

#!/bin/bash
git fetch -p
git branch --merged | grep -v "\*" | grep -v main | grep -v develop | xargs -n 1 git branch -d

List Branches with Last Commit

#!/bin/bash
git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) %(committerdate:relative) %(authorname)'

Interactive Rebase Script

#!/bin/bash
BASE=$1
git fetch origin
git rebase -i $BASE

Automated Tagging

#!/bin/bash
VERSION=$1
BRANCH=${2:-main}
git checkout $BRANCH
git pull origin $BRANCH
git tag -a "v$VERSION" -m "Release version $VERSION"
git push origin "v$VERSION"

Release with Tests & Build

#!/bin/bash
BRANCH=${1:-main}
git checkout $BRANCH
git pull origin $BRANCH
npm run lint
npm test
read -p "Tests passed? Continue release? (y/n): " confirm
if [[ $confirm == "y" ]]; then
    ./build.sh
    git tag -a "v$(date +'%Y.%m.%d.%H%M')" -m "Automated release"
    git push origin $BRANCH --tags
fi

Delete Remote Tag

#!/bin/bash
TAG=$1
git tag -d $TAG
git push origin :refs/tags/$TAG

Clean Dangling Objects

#!/bin/bash
git fsck --full
git prune
git gc --aggressive --prune=now

Find Large Files

#!/bin/bash
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sort -n -k3 |
tail -n 20

Reflog Cleanup

#!/bin/bash
git reflog expire --expire=now --all
git gc --prune=now

Repository Size Summary

#!/bin/bash
git count-objects -vH

Sync Fork with Upstream

#!/bin/bash
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Pre-push Hook

#!/bin/bash
echo "Running lint, test, and security checks..."
npm run lint && npm test && npm run security-check
if [ $? -ne 0 ]; then
    echo "Push aborted: Lint/Test/Security failed"
    exit 1
fi

Commit Message Enforcement

#!/bin/bash
pattern="^(feat|fix|docs|style|refactor|test|chore|perf)\(.*\): .+"
if ! grep -qE "$pattern" "$1"; then
    echo "Invalid commit message format!"
    exit 1
fi

Track Large Files

#!/bin/bash
git lfs install
git lfs track "*.zip" "*.mp4" "*.mov"
git add .gitattributes
git commit -m "Track large files with Git LFS"
git push

Pull LFS Safely

#!/bin/bash
git lfs fetch --all
git lfs checkout

Clean Unused LFS Objects

#!/bin/bash
git lfs prune

Daily Commits Report

#!/bin/bash
git log --since=midnight --pretty=format:"%h %an %s"

Largest Files in Repo

#!/bin/bash
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sed -n 's/^blob //p' |
sort -n -k2 |
tail -n 50

Inactive Branch Report

#!/bin/bash
git for-each-ref --format='%(refname:short) %(committerdate:relative)' refs/heads/ | sort -k2

Contribution Stats

#!/bin/bash
git shortlog -sne

Pre-merge Sanity Check

#!/bin/bash
BRANCH=$1
git fetch origin $BRANCH
git checkout $BRANCH
npm run lint && npm test
if [ $? -ne 0 ]; then
    echo "Merge blocked due to failed tests/lint"
    exit 1
fi

Auto Rebase Before Push

#!/bin/bash
BRANCH=$(git symbolic-ref --short HEAD)
git fetch origin
git rebase origin/main
if [ $? -ne 0 ]; then
    echo "Rebase conflicts detected! Resolve manually."
    exit 1
fi
git push origin $BRANCH

Auto Merge Development into Feature Branch

#!/bin/bash
FEATURE=$1
git checkout $FEATURE
git pull origin $FEATURE
git merge develop
git push origin $FEATURE