Skip to content

Instantly share code, notes, and snippets.

@techiev2
Last active June 23, 2020 15:44
Show Gist options
  • Save techiev2/c47be78a739104e3e8061b92396d5c50 to your computer and use it in GitHub Desktop.
Save techiev2/c47be78a739104e3e8061b92396d5c50 to your computer and use it in GitHub Desktop.
git subtree management helper functions
function gstup() {
if [[ -z $1 ]]; then
echo -e "A subtree name is required"
return
fi
if [[ -z $2 ]]; then
echo -e "Subtree's branch name is required"
return
fi
# Stash local changes to prevent collisions
git stash
# Pull change from remote
git pull -s subtree "$1" "$2" --no-edit
# Do a reset to HEAD~1 to skip the merge commit
git reset HEAD~1
# Add the incoming files to a single changeset
git add .
treeCommit="$(git log --format="{%h by %an} | %s" -n 1 $1)"
msg="From [$1/$2] :: ${treeCommit}"
# Create a new commit
git commit --amend -m "$msg"
git stash pop
}
function gstadd() {
if [[ $1 == 'h' ]] || [[ $1 == '--help' ]] {
printSubTreeAddHelp
return
}
if [[ -z $1 ]]; then
echo -e "Missing subtree repo url"
return
fi
if [[ -z $2 ]]; then
echo -e "Missing subtree name to use as prefix"
return
fi
if [[ -z $3 ]]; then
echo -e "Missing subtree branch to track"
return
fi
git subtree add --prefix="$2/" "$2" "$1" "$3"
}
function printSubTreeAddHelp() {
echo -e "usage: gstadd <repo url> <local path name> <branch to track>"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment