Automatically sync commits from
main/mastertodevelopmentwhen both branches are protected
# 1. Download the script
curl -O https://raw.githubusercontent.com/ijavidilo/trim_branches/main/trim_branches.sh
chmod +x trim_branches.sh
# 2. Run with your repository (Interactive mode - NEW!)
./trim_branches.sh -i
# 3. Or run with direct parameters
./trim_branches.sh https://github.com/your-user/your-repo.git ghp_your_token_hereThat's it! The script will automatically create a Pull Request to sync the branches.
Problem: You have protected main and development branches, can't push directly
Solution: Automatically creates a temporary branch and Pull Request
- π₯ Clones your repository
- π Auto-detects main branch (
mainormaster) - πΏ Auto-detects development branch (
development,develop, ordev) - π± Creates temporary branch from development
- π Syncs commits from main β temporary branch
- π€ Push and creates Pull Request automatically
- π§Ή Cleans up temporary files
- β Git installed and configured
- β Bash (Git Bash on Windows)
- β GitHub Token with repository permissions
- Go to GitHub Settings β Tokens
- "Generate new token (classic)"
- Select: βοΈ repo (Full control)
- Copy the token (starts with
ghp_)
# Interactive mode - asks for URL and token
./trim_branches.sh -i
# Interactive with custom branch
./trim_branches.sh -i feature/my-sync
# Interactive with all options
./trim_branches.sh -i feature/sync "My Name" "my@email.com"# Basic syntax
./trim_branches.sh <repo_url> <token>
# Examples
./trim_branches.sh https://github.com/my-team/project.git ghp_1234567890
./trim_branches.sh https://github.com/my-team/project.git ghp_1234567890 feature/my-sync
./trim_branches.sh https://github.com/my-team/project.git ghp_1234567890 feature/sync "My Name" "my@email.com"./trim_branches.sh --help # Show help
./trim_branches.sh --version # Show version$ ./trim_branches.sh https://github.com/team/project ghp_token123
[INFO] Starting branch synchronization process
[INFO] Git Host: github.com
[INFO] Repository: team/project
[INFO] Main branch detected: master
[INFO] Development branch detected: development
[INFO] Commits in master not in development: 5
[INFO] Creating branch feature/trim_branches from development...
[INFO] Rebasing master onto feature/trim_branches...
[INFO] Preserving original commit dates and authorship...
[SUCCESS] All conflicts resolved automatically with rebase strategy
[INFO] Pushing branch feature/trim_branches...
[SUCCESS] Pull Request created successfully!
[SUCCESS] URL: https://github.com/team/project/pull/42
=== FINAL SUMMARY ===
Repository: team/project
Main branch: master
Development branch: development
Feature branch: feature/trim_branches
Commits synchronized: 5
Pull Request: https://github.com/team/project/pull/42
=============================- π― Interactive mode - User-friendly prompts with validation
- π Smart branch detection - Auto-detects
main/masteranddevelopment/develop/dev - β URL validation - Validates GitHub URLs and tokens
- π‘οΈ Works with protected branches
- π Automatic conflict resolution
- π€ Creates Pull Request automatically
- π Compatible with GitHub.com and GitHub Enterprise
- π§Ή Automatic cleanup of temporary files
- π¨ Colorized output - Clear visual feedback with final summary
- π Preserves commit dates - Maintains original authorship and commit timestamps
- π§ Non-interactive git operations - Prevents editor prompts during automation
- β Cross-platform (Windows, macOS, Linux)
| Parameter | Required | Description | Example |
|---|---|---|---|
repository_url |
β | Repository URL | https://github.com/team/project.git |
personal_access_token |
β | Authentication token | ghp_xxxxxxxxxxxx |
feature_branch_name |
β | Temporary branch name | feature/sync-branches |
git_user_name |
β | Name for commits | "John Doe" |
git_user_email |
β | Email for commits | "john@company.com" |
Note: If you don't specify name/email, it uses your global Git configuration
1. π₯ Clones repository
2. π Auto-detects main branch (main/master)
3. πΏ Auto-detects development branch (development/develop/dev)
4. π± Creates temporary branch from development
5. π Applies commits from main β temporary branch (preserving original dates)
6. β
Resolves conflicts automatically (favoring main branch changes)
7. π€ Push and creates Pull Request with detailed summary
8. π¨ Shows colorized final summary with all details
9. π§Ή Cleans up temporary files
- Branches without common history: Handled automatically using merge strategy
- Conflicts: Resolved automatically by prioritizing the main branch changes
- Multiple executions: Recreates the branch if it already exists
- Commit history preservation: Original commit dates and authorship are maintained
- Large repositories: Efficient handling with smart branch detection and minimal cloning
The script uses --committer-date-is-author-date during rebase operations to ensure:
- β Original commit timestamps are preserved
- β Author information remains intact
- β Historical timeline is maintained accurately
- β No confusion about when changes were actually made
When conflicts occur during synchronization:
- Uses
-X theirsstrategy to favor main branch changes - Automatically resolves file conflicts without manual intervention
- Preserves the intent of main branch updates
- Continues rebase process seamlessly
- Colorized messages: Info (blue), success (green), warnings (yellow), errors (red)
- Detailed final summary: Repository info, branches, commit count, PR link
- Progress indicators: Clear status at each step of the process
[ERROR] API connectivity test failed (HTTP 401)Solution: Regenerate your token at GitHub β Settings β Tokens
[ERROR] Authentication failed or insufficient permissionsSolutions:
- Make sure to check βοΈ repo when creating the token
- For GitHub Enterprise: authorize SSO at Settings β Applications
[ERROR] Repository not foundSolutions:
- Verify the repository URL is correct
- Confirm you have access to the repository
[ERROR] Development branch not found (development/develop/dev)Solution: Create a development branch:
git checkout -b development # or 'develop' or 'dev'
git push origin developmentProblems? Open an issue with:
- The command you executed
- The complete error you see
- Your operating system
MIT License - See LICENSE for details.
β If it helped you, give the repo a star!
Created by: ijavidilo