Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created December 8, 2022 23:49
Show Gist options
  • Save elfsternberg/844f7f26720a8bf87e51a6bc8caf5a8b to your computer and use it in GitHub Desktop.
Save elfsternberg/844f7f26720a8bf87e51a6bc8caf5a8b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o errexit
set -o nounset
shopt -s nullglob
function _die() {
echo "ERROR $? IN ${BASH_SOURCE[0]} AT LINE ${BASH_LINENO[0]}" 1>&2
exit 1
}
trap _die ERR
_usage() {
cat <<EOF
Create a new branch from master in this folder:
git stem <new branch name>
Create a new branch from master in a new worktree:
git stem <new branch name> <new folder name>
Do not include path info in the new folder name; it will automatically be
created as a sibling folder of the current folder.
EOF
}
if [[ $# -eq 0 ]]; then
_usage
exit 1
fi
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
_usage
exit 0
fi
PROJECT_ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
if [[ -z "$PROJECT_ROOT" ]]; then
echo "Git repository not found."
echo ""
_usage
exit 1
fi
if [[ $# -eq 1 ]]; then
cd "$PROJECT_ROOT" || exit 1
git checkout -b "$1" master
exit 0
fi
if [[ $# -eq 2 ]]; then
cd "$PROJECT_ROOT" || exit 1
git worktree add -b "$1" "../$2" master
exit 0
fi
_usage
exit 1
# COPYRIGHT:
# This program is copyrighted (c) 2018
# Elf M. Sternberg ([email protected])
#
# LICENSE:
# https://creativecommons.org/licenses/by/4.0/
#
# This is free software released under the CC-BY license. Users of
# this software enjoy the following rights and responsibilities:
#
# Share — You may copy and redistribute the material in any medium or format
#
# Adapt — You may remix, transform, and build upon the material for
# any purpose, even commercially.
#
# Attribution — You must give appropriate credit, provide a link to
# the license, and indicate if changes were made. You may do so in any
# reasonable manner, but not in any way that suggests the licensor
# endorses you or your use.
#
# You may not employ technological measures or legal terms that
# legally prevent others from doing anything the license permits. THE
# SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@elfsternberg
Copy link
Author

I find myself creating new branches, and new worktrees, all the time, and always from the master branch. So much, in fact, that I got kinda tired of accidentally branching off some current branch, or of putting the worktree in the wrong place. I always want my worktree branches to be siblings of my master copy.

This script handles both of those cases, giving me an easy-to-memorize and easy-to-use paradigm for creating new branches and new worktrees for the most common case.

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