Skip to content

Instantly share code, notes, and snippets.

@c3mdigital
Last active May 16, 2016 19:16
Show Gist options
  • Save c3mdigital/65bb2488e4c19ba161bae2863d3098ab to your computer and use it in GitHub Desktop.
Save c3mdigital/65bb2488e4c19ba161bae2863d3098ab to your computer and use it in GitHub Desktop.
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
#!/bin/sh

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

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


POST_PUSH="$GIT_DIR_/hooks/post-push"


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:
#!/bin/bash
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'
DIR=`pwd`
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 $MESSAGE
echo 'Will be used'

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

NEW_DIR=`pwd` 
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