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.
git clone https://github.com/Alidadei/git-sync-script.git
Copy the example file and add your repo paths:
# Windows
copy repos.example.txt repos.txt
# Linux / macOS
cp repos.example.txt repos.txtEdit 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
# Windows
git-auto-sync.bat
# Linux / macOS
chmod +x git-auto-sync.sh
./git-auto-sync.shCheck the log:
# Windows
type git-auto-sync.log
# Linux / macOS
cat git-auto-sync.logWindows (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 -eAdd this line:
*/10 * * * * /path/to/git-sync-script/git-auto-sync.sh
For each repo in repos.txt, the script runs:
git add -A— stage all changesgit commit— auto commit with timestamp (skipped if nothing to commit)git pull --rebase --autostash— pull remote changesgit push— push to remote
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)
- 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
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- Requires
gitto be in PATH - If behind a proxy, configure git proxy:
git config --global http.proxy http://127.0.0.1:PORT repos.txtandgit-auto-sync.logare 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 仓库。支持 Windows、Linux 和 macOS。通过定时任务每隔 N 分钟自动执行 commit、pull 和 push。
已在 Windows 上测试通过。 Linux 和 macOS 的脚本尚未测试。
git clone https://github.com/Alidadei/git-sync-script.git
复制示例文件并填入你的仓库路径:
# 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
# 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.logWindows(每 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 中的每个仓库依次执行:
git add -A— 暂存所有变更git commit— 自动提交(无变更则跳过)git pull --rebase --autostash— 拉取远程更新并变基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.txt和git-auto-sync.log已加入.gitignore,不会上传到 GitHub- 本工具仅适用于简单的单分支(main/master)项目。 不适用于多分支开发、需要合并冲突处理或多人协作等复杂场景。如果发生 rebase 冲突,脚本会静默失败,需要手动解决冲突。