Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.