Search code examples
gitpushgithooksgit-push

Push all commits of a branch individually, one after one


Is there an easy way to push all commits of a feature branch (starting from, say, master) individually, one after one, so that each commit triggers a push hook on the remote side? This is useful for "test-first" scenarios when you implement and commit first a failing test and then a fix.

I know I can do git push sha:remote-ref-name, but it's tedious to do manually.


Solution

  • This script should work:

    #!/bin/bash
    
    if [ ! -d .git ]; then
        echo "$(basename $0): not a git directory." 1>&2
        exit 1
    fi
    
    # lbranch - name of local branch
    # remote  - name of remote
    # rbranch - name of remote branch
    lbranch=$(git rev-parse --abbrev-ref HEAD)
    remote=$(git config branch.${lbranch}.remote)
    rbranch=$(git config branch.${lbranch}.merge)
    rbranch=${rbranch/refs\/heads\//}
    
    for rev in $(git rev-list --reverse ${lbranch} --not --remotes);
    do
        git push ${remote} ${rev}:${rbranch}
    done