Skip to content

Instantly share code, notes, and snippets.

@diiq
Last active December 30, 2015 12:59
Show Gist options
  • Select an option

  • Save diiq/7832635 to your computer and use it in GitHub Desktop.

Select an option

Save diiq/7832635 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Takes only the *staged* changes, moves them to a new branch, commits them,
# and returns to the current branch. The changes stick around, but now they
# can be reviewed and modified separately!
branch=$1
test -z $branch && echo "branch required." 1>&2 && exit 1
# echo "SAVING STAGED CHANGES" &&
git diff --cached > /tmp/commit-to-branch-patch &&
# echo "UNSTAGING AND REVERTING STAGED CHANGES" &&
git reset > /dev/null &&
git apply --reverse /tmp/commit-to-branch-patch > /dev/null &&
# echo "STASHING UNSTAGED CHANGES" &&
git stash > /dev/null &&
# echo "SAVING UNTRACKED FILES" &&
git add -A > /dev/null &&
git diff --cached > /tmp/not-commit-to-branch-patch &&
# echo "REMOVING UNTRACKED FILES" &&
git reset > /dev/null &&
git apply --reverse /tmp/not-commit-to-branch-patch > /dev/null &&
# echo "CREATING NEW BRANCH" &&
current_branch=`git symbolic-ref -q --short HEAD`
git checkout -b $branch > /dev/null &&
# echo "APPLYING PATCH" &&
git apply /tmp/commit-to-branch-patch > /dev/null &&
git add -A > /dev/null &&
# echo "COMMITTING PATCH" &&
git commit &&
# echo pushing branch
git push origin $branch
# echo "RETURNING TO ORIGINAL BRANCH" &&
git checkout $current_branch > /dev/null &&
# echo "RESTORING UNTRACKED FILES" &&
git apply /tmp/not-commit-to-branch-patch > /dev/null &&
git reset > /dev/null &&
# echo "RESTORING UNSTAGED CHANGES" &&
git stash pop > /dev/null &&
# echo "RESTORING STAGED CHANGES" &&
git apply /tmp/commit-to-branch-patch > /dev/null
@ejdyksen
Copy link
Copy Markdown

Should this really push by default? I just got (ever so slightly) burned by that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment