Skip to content

Instantly share code, notes, and snippets.

@cabloo
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cabloo/99ee869bfb3fafe61e59 to your computer and use it in GitHub Desktop.
Save cabloo/99ee869bfb3fafe61e59 to your computer and use it in GitHub Desktop.
Sync with remotes and create new git branch

Setup

  • Put gitbr in /usr/local/bin.
  • If necessary, replace master and origin with the main branch and remote of the repository.
  • Make it executable (chmod +x /usr/local/bin/gitbr).

Usage

  • Run gitbr with no arguments to have the branch name generated for you (e.g. 04-13-001274741-2015).
  • Run gitbr <branch_name_here> to use a specific branch name.
#!/bin/bash
git stash
branch=$(git branch | grep "dev" | tr -d '[[:space:]]')
if [ -z "$branch" ]; then
# no branch that has dev in the name
branch="master"
fi
remote=$(git remote | sed "/origin/d" | head -n 1)
if [ -z "$remote" ]; then
# no remote repo other than origin
remote="origin"
fi
git checkout "$branch"
git pull "$remote" "$branch"
git push origin "$branch"
now=$(date +"%m-%d-%N-%Y")
title=${1:-"$now"}
git checkout -b "$title"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment