Skip to content

Instantly share code, notes, and snippets.

@tonatiuh
Last active August 29, 2015 14:01
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 tonatiuh/612edc89b3bb20ef74bf to your computer and use it in GitHub Desktop.
Save tonatiuh/612edc89b3bb20ef74bf to your computer and use it in GitHub Desktop.
Bash functions that make automate some day to day tasks

Function that automates the creation of a new gitflow branch:

#!/bin/bash

function git_start_feature() {
  story_title=$1
  story_title=${story_title,,} # downcase string
  story_title=${story_title// /-} # replace spaces by dashes
  story_title=${story_title//\//-} # replace slashes by dashes
  story_title=${story_title//[.,:]/} # remove punctuation characters

  git checkout -b feature/$story_title
}

Being on the develop branch, you can just say

$ git_start_feature "this is my new nice feature: that I'm working on with slashes/and .dots"

and a new branch will be created starting from develop, as:

feature/this-is-my-new-nice-feature-that-im-working-on-with-slashes-and-dots
@TheNaoX
Copy link

TheNaoX commented May 7, 2014

Nicely done :)

@daniel-g
Copy link

daniel-g commented May 7, 2014

I would change it to

git start-feature 'something to start here'
OR
git start 'something to start here'

what you need to do is paste the code within your function in ~/git_extensions/git-start-feature , add the path to the PATH variable, then you are done.

Here you have:

~/git_extensions/git-start-feature

#!/bin/bash

story_title=$1
story_title=${story_title// /-} # replace spaces by dashes
story_title=${story_title//\//-} # replace slashes by dashes
story_title=${story_title//[.,:]/} # remove punctuation characters

git checkout -b feature/$story_title

in the .bash_profile file:

export PATH=$HOME/git_extensions:$PATH

then give the file exec permissions:

chmod +x ~/git_extensions/git-start-feature

Then you have it:

git start-feature 'something amazing here'
Switched to a new branch 'feature/something-amazing-here'

@TheNaoX
Copy link

TheNaoX commented May 7, 2014

Isn't this similar to https://github.com/nvie/gitflow ?

@TheNaoX
Copy link

TheNaoX commented May 7, 2014

Well not, it's a lightweight version of it

@tonatiuh
Copy link
Author

tonatiuh commented May 7, 2014

Yeah, it's lighter @TheNaoX.

@daniel-g pretty nice! I like it, so trick there is to prefix the name of the file that contains the script with "git-", and load it in the bash session.

Also they can be taken into account by git by encapsulating the script in a code function name with the prefix "git", I found doc talking about that here: https://coderwall.com/p/wjuoag

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