Skip to content

Instantly share code, notes, and snippets.

@Sharique55
Last active June 21, 2025 17:53
Show Gist options
  • Select an option

  • Save Sharique55/1752103d9195636ec8c82149e1b4e219 to your computer and use it in GitHub Desktop.

Select an option

Save Sharique55/1752103d9195636ec8c82149e1b4e219 to your computer and use it in GitHub Desktop.
🚀 Instantly push every commit! This Git post-commit hook auto-pushes your branch with zero manual effort—set it and forget it.

🚀 Automatic Post-Commit Push Git Hook (for branch feature01)

A smart, ready-to-use post-commit Git hook that automatically pushes your commits to GitHub whenever you commit on your chosen branch!


🌟 What is this?

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.


📋 Features

  • Zero manual push on your special branch—just commit and relax!
  • Logs all push attempts and results in a persistent git_push.log file.
  • Fails gracefully (with logs) if push fails.
  • Does nothing if you’re not on the target branch.

🛠️ Installation & Usage

  1. Copy the script below into a file named .git/hooks/post-commit in your repository.
  2. 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!

#!/bin/bash
# Get current branch name
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Define target branch and remote name
target_branch="tibmig"
remote_url="origin"
# Define log file path (in repo root directory)
log_file="$(git rev-parse --show-toplevel)/git_push.log"
# Timestamp
timestamp=$(date "+%Y-%m-%d %H:%M:%S")
# Only push if on the target branch
if [ "$current_branch" = "$target_branch" ]; then
echo "[post-commit hook] [$timestamp] You are on '$target_branch' branch. Attempting push..." | tee -a "$log_file"
# Perform push and capture output and exit code
push_output=$(git push "$remote_url" "$target_branch" 2>&1)
push_exit_code=$?
# Log the result
if [ $push_exit_code -eq 0 ]; then
echo "[post-commit hook] [$timestamp] Push successful." | tee -a "$log_file"
else
echo "[post-commit hook] [$timestamp] Push failed with error:" | tee -a "$log_file"
echo "$push_output" | tee -a "$log_file"
fi
else
echo "[post-commit hook] [$timestamp] Not on '$target_branch' branch. No push executed." | tee -a "$log_file"
fi
@Sharique55
Copy link
Author

💥 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment