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.
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.
- 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
- PowerShell
- Git installed and configured
- A Git repository with changes to commit
- Download the
Split-GitCommit.ps1script - Place it in your repository or a convenient location
- Ensure your execution policy allows running scripts:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Navigate to your repository
cd D:\your-repository
# Run with default settings (10 files per commit)
.\Split-GitCommit.ps1# 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"| 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 |
- Verification: Checks if you're in a valid Git repository
- Detection: Collects all changed files (staged, unstaged, untracked)
- Planning: Calculates how many commits will be created
- Confirmation: Shows a summary and asks for confirmation
- Processing: For each batch:
- Stages the specified number of files
- Creates a numbered commit
- Pushes to the remote repository
- Completion: Shows a summary of all operations
================================================
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
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-
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
-
Use meaningful commit messages: Customize the
-CommitMessageparameter to describe what changes you're making -
Test with a small batch first: Try with a small number of files to ensure everything works as expected
-
Ensure clean working directory: Resolve any merge conflicts before running the script
-
Check remote connectivity: Ensure you have push access to the remote repository
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
.gitignorerules
Issue: Network timeout during push
- Solution: Reduce the
FilesPerCommitvalue or check your internet connection
This script is provided as-is for use in managing Git repositories.