Skip to content

kimtth/powershell-git-commit-with-split

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”€ 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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published