Created
April 29, 2012 13:37
-
-
Save motemen/2550483 to your computer and use it in GitHub Desktop.
Commit a file to another branch
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/sh | |
BRANCH=$1 | |
FILE=$2 | |
if [ -z "$BRANCH" ]; then | |
echo "Usage: $0 <branch> <file>" | |
exit 1 | |
fi | |
if [ ! -e "$FILE" ]; then | |
echo "File does not exist:" $FILE | |
exit 1 | |
fi | |
if ((git diff --exit-code && git diff --cached --exit-code) > /dev/null); then | |
: | |
else | |
echo 'Index or working directory dirty' | |
exit 1 | |
fi | |
git read-tree refs/heads/$BRANCH | |
git update-index --add $FILE | |
TREE=$(git write-tree) | |
COMMIT=$(echo added file $FILE | git commit-tree $TREE -p refs/heads/$BRANCH) | |
echo 'Created commit:' $COMMIT | |
git update-ref refs/heads/$BRANCH $COMMIT | |
echo 'Updated branch:' $BRANCH | |
git reset HEAD | |
git clean -f $FILE | |
echo 'Cleaned up' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment