This repository provides a Bash script that automatically commits and pushes multiple local Git repositories.
It is designed for scenarios such as daily learning notes, research backups, or log synchronization.
- 🔄 Support multiple repositories at once
- 🕒 Auto-generate commit messages with timestamps
- 📝 Write logs for each repository
- 🛠️ Easy to extend (just add new repositories to the list)
.
├── README.md # Documentation
├── auto_commit_example.sh # Example script to auto-commit/push multiple repos
├── LICENSE # License
├── .gitignore # Ignore rules (includes macOS/AppleDouble files)
└── example.png # Example screenshot used in README
Note: System-generated files (e.g., ._* files) and user-specific sample files (e.g., under ~/Library/LaunchAgents) are intentionally omitted.
#!/bin/bash
cd "the path of the directory you want to push"
git add .
git commit -m "auto-commit: $(date '+%Y-%m-%d %H:%M:%S')"
git push -u origin main
echo "Success at $(date)" >> the path of the log file
cd "another path"
git add .
git commit -m "auto-commit: $(date '+%Y-%m-%d %H:%M:%S')"
git push -u origin main
echo "Success at $(date)" >> another log file path
#...you can add more repositories, the format is the same- #!/bin/bash: This line specifies the interpreter to use for the script, which is Bash.
- cd "the path of the directory you want to push" This line changes the current directory to the specified path.
- git add . This line stages all changes in the current directory for the next commit.
- git commit -m "auto-commit: $(date '+%Y-%m-%d %H:%M:%S')" This line commits the staged changes with a message including the current timestamp.
- git push -u origin main This line pushes the committed changes to the remote repository named "origin" on the "main" branch.
- echo "Success at $(date)" >> the path of the log file This line appends a success message with the current timestamp to the specified log file.
-
step1:
nano /Users/yourusername/auto_commit.sh
- add the script to the file
- save the file
- exit the editor
-
step2:
chmod +x /Users/yourusername/auto_commit.sh
- make the script executable
-
step3:
nano ~/Library/LaunchAgents/com.yourusername.auto_commit.plist- create the plist file
-
step4:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.yourusername.auto_commit</string> <key>ProgramArguments</key> <array> <string>/Users/yourusername/auto_commit.sh</string> </array> <key>RunAtLoad</key> <true/> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>4</integer> <!-- 北京时间凌晨4点 --> <key>Minute</key> <integer>0</integer> </dict> <key>StandardOutPath</key> <string>/Users/yourusername/auto_commit.log</string> <key>StandardErrorPath</key> <string>/Users/yourusername/auto_commit.error.log</string> </dict> </plist>
- save the file
- exit the editor
-
step5:
launchctl load ~/Library/LaunchAgents/com.yourusername.auto_commit.plist- load the plist file
-
step6(OPTIONAL):
launchctl start com.yourusername.auto_commit
- run the script once right away
-
step7(OPTIONAL):
cat /Users/yourusername/auto_commit.log
- check the log file
