Skip to content

Instantly share code, notes, and snippets.

Last active May 16, 2016 19:16
What would you like to do?
Git pseudo post-push hook for updating supermodel when changes are made and pushed to submodule

Git post push hook for updating supermodule when submodule is updated

  • Create file named git-push-wh with the following content and add it to your path

GIT_DIR_="$(git rev-parse --git-dir)"

BRANCH="$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD))"


git pull --rebase origin $BRANCH
git push origin $BRANCH

test $? -eq 0 && test -x "$POST_PUSH" &&
    exec "$POST_PUSH" "$BRANCH" "$@"
  • chmod +x to the new file to make it executable
  • create the post-push hook by creating a file named post-push and adding it to the parent repo root .git/modules/themes/pfw-wp-puff-pastry-parent-theme/hooks with the following content:
set -e

echo '-> Navigate to the repo root'
ROOT=$(git rev-parse --show-cdup)
if [[ '' != $ROOT ]]; then cd $ROOT; fi

echo '-> Check that this is a submodule'
echo `pwd`;
if [ ! -f .git ]; then echo "You must invoke me from inside a submodule."; exit 1; fi

echo '-> Get a reference to the current directory'
BRANCH=`git rev-parse --abbrev-ref HEAD`

echo '-> Get commit message'

MESSAGE=$( git log -1 HEAD --pretty=format:%s )

echo 'Your last commit message'
echo 'Will be used'

echo '-> Update supermodule (puffpastry repo) from submodule'
cd ../..

git pull --rebase origin master

echo '-> Commit submodule change into puffpastry repo, in the master branch'
git add $DIR
git commit -m "$MESSAGE"
git push origin master

echo '-> Go back to where you came from'
cd -

git checkout $BRANCH
  • Again make the hook executable by running chmod +x post-push

Now you can push your changes to the submodule by running git push-wh from within the submodule directory and it will update the changes to the supermodule parent repo and push up to beansalk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment