How can I automatically push after committing in Git?

55,650

Solution 1

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.

Solution 2

If you start using more than the master branch, you might want to automatically push the current branch. My hook (.git/hooks/post-commit) looks like this:

#!/usr/bin/env bash

branch_name=$(git symbolic-ref --short HEAD)
retcode=$?
non_push_suffix="_local"

# Only push if branch_name was found (my be empty if in detached head state)
if [ $retcode -eq 0 ] ; then
    #Only push if branch_name does not end with the non-push suffix
    if [[ $branch_name != *$non_push_suffix ]] ; then
        echo
        echo "**** Pushing current branch $branch_name to origin [i4h post-commit hook]"
        echo
        git push origin $branch_name;
    fi
fi

It pushes the current branch, if it can determine the branch name with git symbolic-ref.

"How to get current branch name in Git?" deals with this and other ways to get the current branch name.

An automatic push for every branch can be disturbing when working in task branches where you expect some sausage making to happen (you won't be able to rebase easily after pushing). So the hook will not push branches that end with a defined suffix (in the example "_local").

Solution 3

Create a file named "post-commit" in the .git/hooks directory with the contents "git push". Though if you want to automatically provide a password, a modification will be needed.

Solution 4

This git-autopush script allows you to setup a post-commit hook, similar to what has been recommended in "How configure automatic pushing?".
But for the passphrase, you need to run a ssh-agent.

Share:
55,650
ulu
Author by

ulu

Developer on the .Net platform, currently building Chpokk, a .Net IDE (http://chpokk.apphb.com/).

Updated on December 06, 2021

Comments

  • ulu
    ulu over 2 years

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

  • ulu
    ulu over 12 years
    Couldn't make ssh-agent remember my passphrase, so had to make it empty. Hope my wife won't hack my account :)
  • Asclepius
    Asclepius about 10 years
    Speaking of customization, what if I want to push some but not all branches in this manner? For example, I want to auto-push only the branches that have a corresponding remote branch noted in .git/config with the prefix feature/xy/.
  • Yada
    Yada almost 9 years
    git push --all origin
  • oyalhi
    oyalhi almost 8 years
    For the first line, i had to use #!/bin/sh to make it work. Otherwise it kept saying: error: cannot run .git/hooks/post-commit: No such file or directory. Thank you, I like your solution best.
  • i4h
    i4h almost 8 years
    Thank you for the comment, @oyalhi , I updated the shebang line in the answer. It should port better now!
  • webcpu
    webcpu over 6 years
    chmod +x .git/hooks/post-commit
  • Tino
    Tino over 5 years
    There is no need for ssh-agent, just use another passphrase-less git-only ssh-key: ssh-keygen -t ed25519 -f ~/.ssh/id_pushonly. echo $'\nHost pushonly\nHostname DESTINATION\nIdentityFile ~/.ssh/id_pushonly\n' >> ~/.ssh/config. On DESTINATION configure git-shell as shown in superuser.com/a/444899/72223 using pubkey from ~/.ssh/id_pushonly.pub. The needed git-URL is something like git@pushonly:path/to/repo.git. To debug: ssh git@pushonly COMMAND must run git-shell -c COMMAND on DESTINATION. For COMMAND see man git-shell
  • VonC
    VonC over 5 years
    @Tino Thank you. Why are you using the digital signature scheme -t ed25519? I use generally -t rsa, although recently I have to add -m PEM to ssh-keygen (stackoverflow.com/a/53645530/6309, stackoverflow.com/a/53729009/6309).
  • VonC
    VonC over 5 years
    @Tino I understand rsa is slower, less secure (if its length is lower than 2048 bits: bagja.net/blog/upgrade-ssh-key-to-ed25519.html), but I deal at work with older openssh servers which might not interpret correctly an ed255519 signature.
  • Tino
    Tino over 5 years
    I primarily use ed25519 because it gives short and handy lines for ~/.ssh/authorized_keys. Also it is very interesting what DJB writes about ed255519: Secure against side channels (Spectre), less CPU, etc. BTW, when dealing with old sshds, I usually create a special key for them and then configure this in ~/.ssh/config.
  • otto
    otto about 4 years
    hello i get this error: .git/hooks/post-commit: line 3: unexpected EOF while looking for matching ``' .git/hooks/post-commit: line 17: syntax error: unexpected end of file
  • i4h
    i4h about 4 years
    Hey, indeed there was a lost backtick. I updated the answer, please try again
  • Gagan
    Gagan over 3 years
    If you don't want to store the password then also this approach works very well. Only thing after every commit you have to type the password.
  • Peter Mortensen
    Peter Mortensen over 2 years
    What kind of modification? Perhaps extend the answer (and/or point to some reference or Stack Overflow post)?
  • Peter Mortensen
    Peter Mortensen over 2 years
    Why #!/usr/bin/env bash? What is the meaning of it (what is it supposed to do)? Is it to use Bash instead of Dash (sh)? On what system? Ubuntu 16.04 (Xenial Xerus)? Why is it different from the default #!/usr/sh?
  • Peter Mortensen
    Peter Mortensen over 2 years
    What is "window task scheduler"? Is this on Windows? Do you mean Windows Task Scheduler? Or something else? You could also provide a screenshot to supplement the description (perhaps even free-hand circles (no joke)). Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).