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.
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.
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.
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").
Here are simple instructions for pushing/pulling without providing passphrase over SSH for people using Linux and Windows (Git Bash)
On your client:
Check out if you have SSH keys generated:
$ ls ~/.ssh/id_rsa.pub; ls ~/.ssh/id_dsa.pub
/c/Users/Cermo/.ssh/id_rsa.pub <-- I have RSA key
ls: cannot access '/c/Users/Cermo/.ssh/id_dsa.pub': No such file or directory
If you don't have any key (two "ls: cannot access ..." lines), generate a new one. If you have any of the keys, skip this step.
$ ssh-keygen.exe
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Cermo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): <-- press Enter
Enter same passphrase again: <-- press Enter
Copy your key to remote server from which you want to pull or push using git:
$ ssh-copy-id user_name@server_name
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to
filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you
are prompted now it is to install the new keys
user_name@server_name's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'user_name@server_name'"
and check to make sure that only the key(s) you wanted were added.
Note: You will have to provide a password during this operation. After that, your pull/push operations won't request a password.
Note 2: You have to log in to the server using user_name at least once before using this procedure (the home directory, to which SSH keys are copied, is created during the first login).