Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 900
ulu Avatar asked Oct 28 '11 06:10

ulu


People also ask

Can we automate git push?

Wouldn't it be great if we could make whole git process automated instead of typing every time same git commands to push file on to github repository. We can do a simple hack to automate the whole process with just one bash command. Yes!

Should you git push after every commit?

Typically pushing and pulling a few times a day is sufficient. Like @earlonrails said, more frequent pushes means less likelihood of conflicting changes but typically it isn't that big a deal. Think of it this way, by committing to your local repository you are basically saying "I trust this code. It is complete.


4 Answers

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.

like image 56
Mark Longair Avatar answered Oct 09 '22 08:10

Mark Longair


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").

like image 23
i4h Avatar answered Oct 09 '22 09:10

i4h


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.

like image 32
Colin R Avatar answered Oct 09 '22 09:10

Colin R


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.

like image 44
VonC Avatar answered Oct 09 '22 09:10

VonC