Skip to content

Instantly share code, notes, and snippets.

@motemen
Created April 29, 2012 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/2550483 to your computer and use it in GitHub Desktop.
Save motemen/2550483 to your computer and use it in GitHub Desktop.
Commit a file to another branch
#!/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