git-auto-follow
v0.2.0
Published
Auto fetch and pull the latest code from git repo
Readme
Git Auto Follow
🚀 An elegant CLI tool for automatically monitoring and pulling the latest code from Git repositories.
✨ Features
- 🔄 Auto Monitoring: Check remote repository for new commits at fixed intervals
- 📥 Smart Pulling: Automatically execute
git pullwhen new commits are detected - 🎣 Script Hooks: Support for pre-pull and after-pull custom scripts
- 🔗 Multiple Hook Formats: Hooks can be script files or arrays of commands
- ⚙️ Flexible Configuration: Customizable check intervals, monitored branches, etc.
- 🎨 Beautiful Output: Friendly colored terminal output and status indicators
- 💾 Persistent Config: Configuration automatically saved to local file
- 📋 CLI Arguments: Support configuration via command line arguments
- 🔧 Multiple Hook Formats: Support for various naming conventions (camelCase, snake_case, etc.)
- 🤖 Daemon Mode: Run as a background daemon with automatic log rotation
- 📝 Log Rotation: Date-based log rotation in
.git-auto-follow/logsdirectory
📦 Installation
npm install -g git-auto-followOr use npx to run directly:
npx git-auto-follow🚀 Quick Start
Run in your Git repository directory:
# Start monitoring directly
npx git-auto-follow
# Or use the full command
npx git-auto-follow start
# Run as daemon with file logging
npx git-auto-follow start --daemon
# or
npx git-auto-follow --daemon📖 Usage
Basic Commands
# Start monitoring (default command)
git-auto-follow
git-auto-follow start
# Show current status
git-auto-follow status
# Show help
git-auto-follow --helpCommand Line Arguments
# Set check interval (30 seconds)
git-auto-follow --interval 30
# Specify hook scripts
git-auto-follow --pre-fetch ./scripts/pre-fetch.sh --after-pull ./scripts/after-pull.sh
# Monitor specific branch
git-auto-follow --branch develop
# Run as daemon with file logging
git-auto-follow --daemon
# Multiple options
git-auto-follow start --interval 60 --branch main --daemonAvailable CLI Options
| Option | Description | Example |
|--------|-------------|---------|
| -i, --interval <number> | Check interval in seconds | --interval 30 |
| --pre-fetch <path> | preFetch hook script path | --pre-fetch ./pre-fetch.sh |
| --after-fetch <path> | afterFetch hook script path | --after-fetch ./after-fetch.sh |
| --pre-pull <path> | prePull hook script path | --pre-pull ./pre-pull.sh |
| --after-pull <path> | afterPull hook script path | --after-pull ./after-pull.js |
| -b, --branch <name> | Branch to monitor | --branch develop |
| -d, --daemon | Run as daemon with file logging | --daemon |
Configuration
You can configure the tool by:
- Using CLI arguments (see Available CLI Options above)
- Creating a configuration file (see Configuration File section below)
- Using default hook scripts (see Hook Scripts section below)
Hook Scripts
Default Hook Discovery
The tool automatically looks for hook scripts in the .git-auto-follow/ directory with these naming patterns:
preFetch hooks (executed before git fetch):
preFetch.js,preFetch.mjs,preFetch.ts,preFetch.shpre_fetch.js,pre_fetch.mjs,pre_fetch.ts,pre_fetch.shpre-fetch.js,pre-fetch.mjs,pre-fetch.ts,pre-fetch.shprefetch.js,prefetch.mjs,prefetch.ts,prefetch.sh
afterFetch hooks (executed after git fetch):
afterFetch.js,afterFetch.mjs,afterFetch.ts,afterFetch.shafter_fetch.js,after_fetch.mjs,after_fetch.ts,after_fetch.shafter-fetch.js,after-fetch.mjs,after-fetch.ts,after-fetch.shafterfetch.js,afterfetch.mjs,afterfetch.ts,afterfetch.sh
prePull hooks (executed before git pull):
prePull.js,prePull.mjs,prePull.ts,prePull.shpre_pull.js,pre_pull.mjs,pre_pull.ts,pre_pull.shpre-pull.js,pre-pull.mjs,pre-pull.ts,pre-pull.shprepull.js,prepull.mjs,prepull.ts,prepull.sh
afterPull hooks (executed after git pull):
afterPull.js,afterPull.mjs,afterPull.ts,afterPull.shafter_pull.js,after_pull.mjs,after_pull.ts,after_pull.shafter-pull.js,after-pull.mjs,after-pull.ts,after-pull.shafterpull.js,afterpull.mjs,afterpull.ts,afterpull.sh
Example Hook Scripts
prePull Script (.git-auto-follow/prePull.js)
// Stop development server before pulling
const { execSync } = require('node:child_process')
console.log('Preparing to pull latest code...')
try {
execSync('pkill -f "npm run dev"', { stdio: 'ignore' })
console.log('Development server stopped')
} catch (error) {
console.log('No development server to stop')
}afterPull Script (.git-auto-follow/after_pull.sh)
#!/bin/bash
echo "Code updated, performing post-pull actions..."
# Install new dependencies
npm install
# Restart development server
npm run dev &
echo "Post-pull actions completed"⚙️ Configuration File
The tool generates a .git-auto-follow.json configuration file in the project root:
{
"interval": 30,
"hooks": {
"preFetch": "./scripts/pre-fetch.sh",
"afterFetch": "./scripts/after-fetch.sh",
"prePull": "./scripts/pre-pull.sh",
"afterPull": "./scripts/after-pull.js"
},
"branch": "main",
"daemon": true
}Using Command Arrays
You can also specify hooks as arrays of commands instead of script files. Commands will be executed sequentially, and if any command fails, execution stops immediately:
{
"interval": 30,
"hooks": {
"preFetch": [
"echo 'Fetching updates...'",
"npm run pre-fetch-check"
],
"prePull": [
"echo 'Preparing to pull...'",
"npm run backup",
"pkill -f 'npm run dev'"
],
"afterPull": [
"npm install",
"npm run build",
"npm run dev &"
],
"afterFetch": [
"echo 'Fetch completed'",
"npm run post-fetch-hook"
]
},
"branch": "main"
}Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| interval | number | 30 | Check interval (seconds) |
| hooks | object | {} | Hooks configuration object |
| hooks.preFetch | string | string[] | - | Hook to run before git fetch |
| hooks.afterFetch | string | string[] | - | Hook to run after git fetch |
| hooks.prePull | string | string[] | - | Hook to run before git pull |
| hooks.afterPull | string | string[] | - | Hook to run after git pull |
| branch | string | current branch | Branch name to monitor |
| daemon | boolean | false | Run as daemon with file logging |
Configuration Priority
- Command line arguments (highest priority)
- Configuration file (
.git-auto-follow.json) - Default hook scripts (
.git-auto-follow/directory) - Default values (lowest priority)
🤖 Daemon Mode
Run git-auto-follow as a daemon process with automatic file logging:
# Start as daemon
git-auto-follow --daemon
# Or with start command
git-auto-follow start --daemonLog Rotation
When running in daemon mode, logs are automatically written to .git-auto-follow/logs/ with the following features:
- Date-based rotation: Each day gets a new log file named
YYYY-MM-DD.log - Automatic switching: The log file automatically switches at midnight
- Console + File: All output is shown in console AND written to the log file
- Clean format: ANSI color codes are stripped from log files for readability
Example log structure:
.git-auto-follow/
├── logs/
│ ├── 2025-01-15.log
│ ├── 2025-01-16.log
│ └── 2025-01-17.log
└── prePull.jsLog file format:
[2025-01-17T10:30:45.123Z] [INFO] 🚀 Starting Git repository monitoring...
[2025-01-17T10:30:45.456Z] [INFO] ⏰ Check interval: 30 seconds
[2025-01-17T10:31:15.789Z] [INFO] 🆕 New commits detected!
[2025-01-17T10:31:16.012Z] [INFO] 📥 Pulling latest code...🎯 Use Cases
- Development Environment Sync: Automatically sync latest code during team collaboration
- Automated Deployment: Simple CI/CD pipeline with custom scripts
- Code Review: Timely access to code changes for review
- Multi-environment Sync: Auto-sync main branch changes in testing environments
🛡️ Security Notes
- Script Permissions: Ensure pre-pull and after-pull scripts have execution permissions
- Conflict Handling: Tool won't automatically handle merge conflicts; will stop pulling on conflicts
- Branch Protection: Recommended for use on protected branches to avoid accidental code overwrites
- Network Dependency: Requires stable network connection to check remote repository
🔧 Development
# Clone project
git clone <repo-url>
cd git-auto-follow
# Install dependencies
npm install
# Development mode
npm run watch
# Build
npm run build📝 License
MIT License
🤝 Contributing
Issues and Pull Requests are welcome!
📞 Support
If you encounter problems or have feature suggestions, please submit an Issue.
