Skip to content

Alidadei/git-sync-script

Repository files navigation

Git Auto Sync Script

English | 中文


English

A simple script to automatically sync multiple local Git repositories. Supports Windows, Linux, and macOS. Runs as a scheduled task / cron job every N minutes to commit, pull, and push changes.

Tested on Windows. Linux and macOS scripts are provided but have not been tested yet.

Quick Start

1. Clone this repo

git clone https://github.com/Alidadei/git-sync-script.git

2. Configure repos

Copy the example file and add your repo paths:

# Windows
copy repos.example.txt repos.txt

# Linux / macOS
cp repos.example.txt repos.txt

Edit repos.txt, one repo path per line:

# Windows
C:\Users\you\project-a
C:\Users\you\project-b

# Linux / macOS
/home/you/project-a
/Users/you/project-b

# Lines starting with # are ignored
# C:\Users\you\paused-project

3. Test manually

# Windows
git-auto-sync.bat

# Linux / macOS
chmod +x git-auto-sync.sh
./git-auto-sync.sh

Check the log:

# Windows
type git-auto-sync.log

# Linux / macOS
cat git-auto-sync.log

4. Set up scheduled task

Windows (every 10 minutes, runs silently in background):

schtasks /create /sc minute /mo 10 /tn "GitAutoSync" /tr "wscript.exe \"C:\path\to\git-sync-script\git-auto-sync-silent.vbs\""

The VBS wrapper (git-auto-sync-silent.vbs) runs the batch script without showing a console window.

Linux / macOS (every 10 minutes):

crontab -e

Add this line:

*/10 * * * * /path/to/git-sync-script/git-auto-sync.sh

What it does

For each repo in repos.txt, the script runs:

  1. git add -A — stage all changes
  2. git commit — auto commit with timestamp (skipped if nothing to commit)
  3. git pull --rebase --autostash — pull remote changes
  4. git push — push to remote

File Structure

git-sync-script/
├── .gitignore
├── README.md
├── git-auto-sync.bat          # Windows sync script
├── git-auto-sync.sh           # Linux/macOS sync script
├── git-auto-sync-silent.vbs   # VBS wrapper to run .bat silently (Windows)
├── repos.example.txt          # Example repo list
├── repos.txt                  # Your repo list (gitignored)
└── git-auto-sync.log          # Sync log (gitignored)

Maintenance

  • Add repo — add a line to repos.txt
  • Remove repo — delete the line
  • Pause a repo — prepend # to the line
  • Change interval — Windows: recreate scheduled task with different /mo; Linux/macOS: edit crontab

Manage scheduled task

Windows:

:: Check status
schtasks /query /tn GitAutoSync

:: Delete task
schtasks /delete /tn GitAutoSync /f

:: Recreate with 2-minute interval
schtasks /create /sc minute /mo 2 /tn GitAutoSync /tr "wscript.exe \"C:\path\to\git-sync-script\git-auto-sync-silent.vbs\""

Linux / macOS:

# View current crontab
crontab -l

# Edit crontab
crontab -e

# Remove the line to stop
# Change */10 to */2 for 2-minute interval

Notes

  • Requires git to be in PATH
  • If behind a proxy, configure git proxy: git config --global http.proxy http://127.0.0.1:PORT
  • repos.txt and git-auto-sync.log are gitignored — they stay local
  • This tool is designed for simple single-branch (main/master) projects only. It is NOT suitable for projects with multiple branches, merge conflicts, or collaborative workflows requiring careful conflict resolution. If a rebase conflict occurs, the script will fail silently — you will need to resolve it manually.

中文

一个简单的脚本,用于自动同步多个本地 Git 仓库。支持 WindowsLinuxmacOS。通过定时任务每隔 N 分钟自动执行 commit、pull 和 push。

已在 Windows 上测试通过。 Linux 和 macOS 的脚本尚未测试。

快速开始

1. 克隆仓库

git clone https://github.com/Alidadei/git-sync-script.git

2. 配置仓库列表

复制示例文件并填入你的仓库路径:

# Windows
copy repos.example.txt repos.txt

# Linux / macOS
cp repos.example.txt repos.txt

编辑 repos.txt,每行一个仓库路径:

# Windows
C:\Users\you\project-a
C:\Users\you\project-b

# Linux / macOS
/home/you/project-a
/Users/you/project-b

# 以 # 开头的行会被跳过
# C:\Users\you\paused-project

3. 手动测试

# Windows
git-auto-sync.bat

# Linux / macOS
chmod +x git-auto-sync.sh
./git-auto-sync.sh

查看日志:

# Windows
type git-auto-sync.log

# Linux / macOS
cat git-auto-sync.log

4. 设置定时任务

Windows(每 10 分钟,静默后台运行):

schtasks /create /sc minute /mo 10 /tn "GitAutoSync" /tr "wscript.exe \"C:\path\to\git-sync-script\git-auto-sync-silent.vbs\""

VBS 包装脚本(git-auto-sync-silent.vbs)会在后台静默运行批处理脚本,不会弹出命令行窗口。

Linux / macOS(每 10 分钟):

crontab -e

添加以下内容:

*/10 * * * * /path/to/git-sync-script/git-auto-sync.sh

工作流程

脚本对 repos.txt 中的每个仓库依次执行:

  1. git add -A — 暂存所有变更
  2. git commit — 自动提交(无变更则跳过)
  3. git pull --rebase --autostash — 拉取远程更新并变基
  4. git push — 推送到远程

文件结构

git-sync-script/
├── .gitignore
├── README.md
├── git-auto-sync.bat          # Windows 同步脚本
├── git-auto-sync.sh           # Linux/macOS 同步脚本
├── git-auto-sync-silent.vbs   # VBS 包装脚本,静默运行 .bat(Windows)
├── repos.example.txt          # 示例仓库列表
├── repos.txt                  # 你的仓库列表(gitignored)
└── git-auto-sync.log          # 同步日志(gitignored)

日常维护

  • 添加仓库 — 在 repos.txt 中加一行路径
  • 删除仓库 — 删掉对应行
  • 暂停某个仓库 — 行首加 #
  • 修改同步间隔 — Windows:删除定时任务后用不同的 /mo 值重新创建;Linux/macOS:编辑 crontab

定时任务管理

Windows:

:: 查看状态
schtasks /query /tn GitAutoSync

:: 删除任务
schtasks /delete /tn GitAutoSync /f

:: 改为每 2 分钟同步一次
schtasks /create /sc minute /mo 2 /tn GitAutoSync /tr "wscript.exe \"C:\path\to\git-sync-script\git-auto-sync-silent.vbs\""

Linux / macOS:

# 查看当前定时任务
crontab -l

# 编辑定时任务
crontab -e

# 删除对应行即可停止
# 将 */10 改为 */2 即可改为每 2 分钟同步一次

注意事项

  • 需要系统 PATH 中有 git 命令
  • 如果使用代理,需配置 git 代理:git config --global http.proxy http://127.0.0.1:端口
  • repos.txtgit-auto-sync.log 已加入 .gitignore,不会上传到 GitHub
  • 本工具仅适用于简单的单分支(main/master)项目。 不适用于多分支开发、需要合并冲突处理或多人协作等复杂场景。如果发生 rebase 冲突,脚本会静默失败,需要手动解决冲突。

About

一个用于自动同步多个本地 Git 仓库的简单脚本。支持 Windows、Linux 和 macOS 系统。作为定时任务/cron 任务每 N 分钟运行一次,以提交、拉取和推送更改。A simple script to automatically sync multiple local Git repositories. Supports Windows, Linux, and macOS. Runs as a scheduled task / cron job every N minutes to commit, pull, and push changes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors