Created
May 9, 2013 21:53
-
-
Save stedolan/5550923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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