git auto commit script

Problem: I almost always use public repos (which are cloned from HTTPS). I even do this if it is my own repos. Sometimes I would like to commit to the repo, but when you have cloned a HTTPS url, you will have to enter your credentials.

This small script does a git add . && git commit -m "my message" && git push

And in the process it rewrites the push URL from https:// to the git@ (ssh) format. Therefor you don't need to think about if you have checked out a HTTPS or a SSH repo version.


 * About: Small script to use if you don't want to do: 
 * git add . && git commit && git push
 * If you use a https URL, then the https URL will 
 * be rewritten to a SSH private URL
 * install: save script as e.g. in a bin/ folder
 * usage: "my commit message"

$git_add = "git add . ";

if (isset($argv[1])) {
    $message = $argv[1];
} else {
    $message = "auto commit";

$git_commit = "git commit -m \"$message\" ";

$origin = git_get_remote_origin ();
$push_url = getSshFromHttps ($origin);

$git_push = "git push $push_url";

function getSshFromHttps($url) {

    $ary = parse_url(trim($url));
    $num = count($ary);

    // Is in the format git@ ...
    if ($num == 1) {
        return $ary['path'];

    // E.g.
    if (isset($ary['scheme']) && $ary['scheme'] == 'git') {
        return "git@$ary[host]:" . ltrim($ary['path'], '/');

    return "git@$ary[host]:" . ltrim($ary['path'], '/');

function git_get_remote_origin () {
    return shell_exec("git config --get remote.origin.url");

This page has been viewed 4124 times. First hit: 30-Oct-2015 13:54:20
Share email, Google+, Twitter, Facebook.
comments powered by Disqus