Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Created May 30, 2014 15:25
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 jtpaasch/9592f201366a32d3e732 to your computer and use it in GitHub Desktop.
Save jtpaasch/9592f201366a32d3e732 to your computer and use it in GitHub Desktop.
Recreate a git branch clean
#!/bin/bash
# We want to clear out the "rc" branch. To do that, we need
# to be on a different branch. We'll get onto "master".
# Does master exist? The first command's status code
# will be "0" if the branch exists.
git show-ref --verify --quiet refs/heads/master
MASTER_EXISTS=$?
# If master doesn't exist, let's fetch it.
if [[ $MASTER_EXISTS != "0" ]]; then
git fetch origin master:master
fi
# Switch to the master branch.
git checkout master
# Now we can delete rc.
# Let's suppress errors. If rc does exist, great, we can delet it.
# If not, we don't need to know that error.
git branch -D rc > /dev/null 2>&1
# At this point, it's safe to create rc and check it out.
git fetch origin rc:rc
git checkout rc
# Do your FOO.
# Now delete master if it didn't exist before,
# to return the system to its original state.
if [[ $MASTER_EXISTS != "0" ]]; then
git branch -D master
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment