Skip to content

ijavidilo/trim_branches

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Git Branch Sync Script

License: MIT Shell Script

Automatically sync commits from main/master to development when both branches are protected

⚑ Quick Start

# 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_here

That's it! The script will automatically create a Pull Request to sync the branches.


🎯 What does this script do?

Problem: You have protected main and development branches, can't push directly
Solution: Automatically creates a temporary branch and Pull Request

  1. πŸ“₯ Clones your repository
  2. πŸ” Auto-detects main branch (main or master)
  3. 🌿 Auto-detects development branch (development, develop, or dev)
  4. 🌱 Creates temporary branch from development
  5. πŸ”„ Syncs commits from main β†’ temporary branch
  6. πŸ“€ Push and creates Pull Request automatically
  7. 🧹 Cleans up temporary files

πŸ“‹ Requirements

  • βœ… Git installed and configured
  • βœ… Bash (Git Bash on Windows)
  • βœ… GitHub Token with repository permissions

πŸ”‘ Create GitHub Token

  1. Go to GitHub Settings β†’ Tokens
  2. "Generate new token (classic)"
  3. Select: β˜‘οΈ repo (Full control)
  4. Copy the token (starts with ghp_)

πŸ’‘ Usage

🎯 Interactive Mode (Recommended - NEW!)

# 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"

πŸ“ Direct Parameters Mode

# 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"

πŸ“‹ Other Options

./trim_branches.sh --help     # Show help
./trim_branches.sh --version  # Show version

πŸ“„ Example Output

$ ./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
=============================

✨ Features

  • 🎯 Interactive mode - User-friendly prompts with validation
  • πŸ” Smart branch detection - Auto-detects main/master and development/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)

πŸ”§ Parameters

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

πŸ“Š How it works?

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

πŸ’‘ Special Cases

  • 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

πŸ”§ Technical Details

πŸ“… Commit Date Preservation

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

πŸ€– Automatic Conflict Resolution

When conflicts occur during synchronization:

  • Uses -X theirs strategy to favor main branch changes
  • Automatically resolves file conflicts without manual intervention
  • Preserves the intent of main branch updates
  • Continues rebase process seamlessly

🎨 Enhanced Output

  • 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

🚨 Common Errors

πŸ” Error 401 - Invalid token

[ERROR] API connectivity test failed (HTTP 401)

Solution: Regenerate your token at GitHub β†’ Settings β†’ Tokens

🚫 Error 403 - No permissions

[ERROR] Authentication failed or insufficient permissions

Solutions:

  • Make sure to check β˜‘οΈ repo when creating the token
  • For GitHub Enterprise: authorize SSO at Settings β†’ Applications

πŸ” Error 404 - Repository not found

[ERROR] Repository not found

Solutions:

  • Verify the repository URL is correct
  • Confirm you have access to the repository

🌿 Development branch doesn't exist

[ERROR] Development branch not found (development/develop/dev)

Solution: Create a development branch:

git checkout -b development  # or 'develop' or 'dev'
git push origin development

πŸ“ž Help

Problems? Open an issue with:

  • The command you executed
  • The complete error you see
  • Your operating system

πŸ“„ License

MIT License - See LICENSE for details.


⭐ If it helped you, give the repo a star!
Created by: ijavidilo

About

Automated script for synchronizing commits from the main branch (`master`/`main`) to the `development` branch when both are protected using branch protection rules.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages