Skip to content

Instantly share code, notes, and snippets.

@brookst
Created November 7, 2016 13:47
Show Gist options
  • Save brookst/af61fd5012bb7b2eb879ac8c6557ebaf to your computer and use it in GitHub Desktop.
Save brookst/af61fd5012bb7b2eb879ac8c6557ebaf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Demo using a hook to pull a file from master branch
# !Run in a temp dir!
set -ue
main () {
echo \> Setup
mkdir work
cd work
git init
echo "Initial state" > file
git add file
echo "Initial config" > master.cfg
git add master.cfg
git commit -m "Initial commit"
cd -
cd work
echo \> Write ref update hook
cat <<-EOF > .git/hooks/post-checkout
#!/bin/bash
set -ue
old_head=\$1
new_head=\$2
branch_checkout=\$3
if [ \$branch_checkout == 1 ]; then
echo "Checkout from branch: \$old_head → \$new_head"
git checkout master master.cfg
else
echo "Checkout file from: \$old_head → \$new_head"
fi
EOF
chmod +x .git/hooks/post-checkout
cd -
echo
echo \> Make a change on a new branch
cd work
git checkout -b dev
echo "Modified state" > file
git commit -m "Modification" file
cd -
echo
echo \> Checkout branches
cd work
git checkout master
grep --color=always . ./*
git checkout dev
grep --color=always . ./*
cd -
echo
echo \> Make a change to master config
cd work
git checkout master
echo "Modified config" > master.cfg
git commit -m "Config change" master.cfg
cd -
echo
echo \> Checkout branches
cd work
git checkout master
grep --color=always . ./*
git checkout dev
grep --color=always . ./*
cd -
}
if [ "${1:-}" = "clean" ]; then
rm -rf work
else
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment