Skip to content

Instantly share code, notes, and snippets.

@stedolan
Created May 9, 2013 21:53
Show Gist options
  • Save stedolan/5550923 to your computer and use it in GitHub Desktop.
Save stedolan/5550923 to your computer and use it in GitHub Desktop.
#!/bin/bash
die () {
echo "$0: $@" > /dev/stderr
exit 1
}
usage () {
echo "Usage: $0 <branchname> <directory>" > /dev/stderr
exit 1
}
[ -z "$(git status --porcelain)" ] || \
die "must be run with clean working directory"
branch="$1"
git show-ref --verify --quiet "refs/heads/$branch" || usage
dir="$2"
[ -e "$dir" ] || usage
[ "$(git rev-parse --show-toplevel)" = "$(pwd)" ] || \
die "must be run from top directory of project"
newbranch="$branch-new-contents"
current="$(git rev-parse --abbrev-ref HEAD)"
set -e
git checkout "$branch"
git merge -s ours -m merge "$current"
git checkout -b "$newbranch"
git merge -s ours -m merge "$branch"
git add -f "$dir"
git commit -m "update"
git filter-branch --subdirectory-filter "$dir" -- HEAD^!
git update-ref -d "refs/original/refs/heads/$newbranch"
git checkout "$branch"
git merge --squash "$newbranch"
git commit -m "update"
git branch -D "$newbranch"
git checkout "$current"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment