kimtth / powershell-git-commit-with-split

πŸͺ“Split big-size repositories into multiple commits to prevent network hang-ups.

Repository from Github https://github.comkimtth/powershell-git-commit-with-splitRepository from Github https://github.comkimtth/powershell-git-commit-with-split

πŸ”€ Git Split Commit Script

A PowerShell script that helps manage large repositories by automatically splitting a large set of changes into multiple smaller commits and pushing them sequentially to a remote repository.

🎯 Problem Statement

When working with large repositories or making extensive changes, committing all files at once can:

  • Create oversized commits that are difficult to review
  • Cause push failures due to size limitations
  • Make it harder to track and revert specific changes
  • Lead to timeout issues with remote repositories

This script solves these problems by automatically batching your changes into manageable commits.

✨ Features

  • Automatic File Detection: Finds all staged, unstaged, and untracked files
  • Configurable Batch Size: Set how many files per commit
  • Sequential Push: Each commit is pushed immediately after creation
  • Progress Tracking: Clear visual feedback on progress
  • Error Handling: Stops on errors with recovery instructions
  • User Confirmation: Previews the operation before executing
  • Colorful Output: Easy-to-read console output with color coding

πŸ“‹ Requirements

  • PowerShell
  • Git installed and configured
  • A Git repository with changes to commit

πŸ“¦ Installation

  1. Download the Split-GitCommit.ps1 script
  2. Place it in your repository or a convenient location
  3. Ensure your execution policy allows running scripts:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

πŸš€ Usage

Basic Usage

# Navigate to your repository
cd D:\your-repository

# Run with default settings (10 files per commit)
.\Split-GitCommit.ps1

Advanced Usage

# Specify number of files per commit
.\Split-GitCommit.ps1 -FilesPerCommit 5

# Custom commit message
.\Split-GitCommit.ps1 -FilesPerCommit 20 -CommitMessage "Feature: Add new components"

# Specify remote and branch
.\Split-GitCommit.ps1 -FilesPerCommit 15 -RemoteName "upstream" -BranchName "main"

# Full customization
.\.Split-GitCommit.ps1 -FilesPerCommit 25 -CommitMessage "Refactor" -RemoteName "origin" -BranchName "develop"

βš™οΈ Parameters

Parameter Type Default Description
FilesPerCommit int 10 Number of files to include in each commit
CommitMessage string "Split commit - batch" Base message for commits (will be numbered)
RemoteName string "origin" Name of the remote repository
BranchName string (current branch) Target branch for pushing commits

πŸ”„ How It Works

  1. Verification: Checks if you're in a valid Git repository
  2. Detection: Collects all changed files (staged, unstaged, untracked)
  3. Planning: Calculates how many commits will be created
  4. Confirmation: Shows a summary and asks for confirmation
  5. Processing: For each batch:
    • Stages the specified number of files
    • Creates a numbered commit
    • Pushes to the remote repository
  6. Completion: Shows a summary of all operations

πŸ“Š Example Output

================================================
Git Split Commit and Push Script
================================================

βœ“ Git repository detected
Using current branch: main
Collecting changed files...
βœ“ Found 47 changed file(s)

Configuration:
  Files per commit: 10
  Total files: 47
  Commits to create: 5
  Remote: origin
  Branch: main

Do you want to proceed? (y/n): y

Starting commit and push process...

[1/5] Processing batch with 10 file(s)...
  Creating commit: Split commit - batch 1/5
  βœ“ Commit created
  Pushing to origin/main...
  βœ“ Pushed successfully

[2/5] Processing batch with 10 file(s)...
...

================================================
βœ“ All commits completed successfully!
================================================

Summary:
  Total commits created: 5
  Total files committed: 47
  All changes pushed to: origin/main

⚠️ Error Handling

If an error occurs during the process, the script will:

  • Display a clear error message
  • Stop execution to prevent further issues
  • Provide recovery instructions

You can then:

# Check current status
git status

# Review recent commits
git log --oneline -n 5

# Check remote configuration
git remote -v

πŸ’‘ Best Practices

  1. Choose appropriate batch size:

    • Small repositories or fast connections: 20-50 files per commit
    • Large repositories or slow connections: 5-15 files per commit
    • Very large files: Consider 1-5 files per commit
  2. Use meaningful commit messages: Customize the -CommitMessage parameter to describe what changes you're making

  3. Test with a small batch first: Try with a small number of files to ensure everything works as expected

  4. Ensure clean working directory: Resolve any merge conflicts before running the script

  5. Check remote connectivity: Ensure you have push access to the remote repository

πŸ”§ Troubleshooting

Issue: "Not a git repository" error

  • Solution: Run the script from within a Git repository directory

Issue: Push fails with authentication error

  • Solution: Ensure your Git credentials are configured and you have push access

Issue: Commit fails

  • Solution: Check for uncommitted merge conflicts or review .gitignore rules

Issue: Network timeout during push

  • Solution: Reduce the FilesPerCommit value or check your internet connection

πŸ“„ License

This script is provided as-is for use in managing Git repositories.

About

πŸͺ“Split big-size repositories into multiple commits to prevent network hang-ups.


Languages

Language:PowerShell 100.0%