Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@TheNaoX TheNaoX commented May 7, 2014

Nicely done :)

@daniel-g

This comment has been minimized.

Copy link

@daniel-g 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

This comment has been minimized.

Copy link

@TheNaoX TheNaoX commented May 7, 2014

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

@TheNaoX

This comment has been minimized.

Copy link

@TheNaoX TheNaoX commented May 7, 2014

Well not, it's a lightweight version of it

@tonatiuh

This comment has been minimized.

Copy link
Owner Author

@tonatiuh 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