gitgithooksgit-pushpost-commit-hook

How can I automatically push after committing in Git?


How do I set Git to automatically push to a remote repository (including automatically providing my passphrase) after each commit to the local repository?


Solution

  • First, make sure that you can push manually without providing your password. If you are pushing over HTTP or HTTPS, that will be a case of either creating a .netrc file with the login details or adding your username and password into the URL for the remote. If you're using SSH, you can either create a keypair where the private key doesn't have a password, or use ssh-agent to cache your private key.

    Then you should create an executable (chmod +x) file in .git/hooks/post-commit that contains the following:

    #!/bin/sh
    git push origin master
    

    ... customizing that line if you want to push to a remote other than origin, or push a branch other than master. Make sure that you make that file executable.