A smart, ready-to-use
post-commitGit hook that automatically pushes your commits to GitHub whenever you commit on your chosen branch!
This is a Bash script for a Git post-commit hook.
Whenever you make a commit on your feature01 branch, the hook will automatically trigger a push to your corresponding remote (origin/feature01).
It also maintains a log file (git_push.log) at the repo root to track push outcomes and errors.
- Zero manual push on your special branch—just commit and relax!
- Logs all push attempts and results in a persistent
git_push.logfile. - Fails gracefully (with logs) if push fails.
- Does nothing if you’re not on the target branch.
- Copy the script below into a file named
.git/hooks/post-commitin your repository. - Make it executable:
chmod +x .git/hooks/post-commit
That’s it! Now every commit on feature01 auto-pushes to origin.
🔒 Note: Git hooks are local—they do NOT sync via Git. Install manually in every repo where you want this automation.
🤓 Example Workflow
# On branch 'feature01'
$ git commit -m "Update feature"
[post-commit hook] [2025-06-17 21:05:02] You are on 'feature01' branch. Attempting push...
[post-commit hook] [2025-06-17 21:05:05] Push successful.💡 Pro Tips
-
You can change the target_branch and remote_url at the top to fit your workflow.
-
Review git_push.log for troubleshooting if a push fails.
🌐 Like this? Star my GitHub and check out more DevOps, Git, and automation hacks!
💥 Auto-push magic for your favorite Git branch—commit, and let the script handle the rest!
Boost your productivity. Automate your workflow. Spread the love with a ⭐ on this Gist!