Git Flow
has become a widely-used and respected branching model in software development.
It's centered around the idea of managing feature branches, along with develop
and main branches for preparing releases. While effective and great for ensuring
a reliable codebase, I found the repetitive nature of Git Flow commands somewhat
burdensome over time. The constant typing of commands like git flow
feature start ...
began to wear on me. To address this, I've refined my
Bash environment to create aliases that eliminate the tedious work.
I got the idea of this little hack when I was working on Dancer2::Plugin::LiteBlog. I often needed to switch between branches and ensure that I was applying changes in the correct place. To streamline my Git workflow, I came up with a set of Bash functions and aliases that made managing Git flow feature branches effortless. It's super simple, and once added to your shell environment, it makes your git flow fu so much painless.
Starting a New Feature with Git Flow
When you're ready to start a new feature, you usually need to ensure that you're branching off from the right place. Typically, the development or devel branch is where all the action happens.
With Git Flow, you should start a new feature from that development branch (in my example below, it will be named devel), with the following command line:
git flow feature start FEATURE_NAME
It's quite verbose, and you should not launch this command unless:
- You are in a git repository.
- The current branch is the development branch.
The git_flow_feature_start
Function
Here is a Bash function that does all the boring stuff for you, and allows you to open a new feature with as less verbosity as possible.
function git_flow_feature_start() {
curbranch=$(git symbolic-ref --short HEAD)
if [ "$curbranch" == "devel" ]; then
if [ -z "$1" ]; then
echo "Please specify a feature name to start."
else
git flow feature start "$1"
fi
else
echo "You can only start a new feature from the 'devel' branch."
fi
}
To use this function, simply call it with the feature name (note that I've added
an alias gfs
that points to that function, as we'll see in the end
of this article):
gfs feature-name
Finishing a Feature with Git Flow
Finishing a feature should be just as seamless, but only if you're on a branch that actually represents a feature. Otherwise, well, it does not make sense.
The git_flow_feature_finish
Function
This function checks that your current branch starts with feature/ before attempting to finish it:
function git_flow_feature_finish() {
curbranch=$(git symbolic-ref --short HEAD)
if [[ $curbranch == feature/* ]]; then
git flow feature finish "${curbranch#feature/}"
else
echo "You can only finish a feature if you are on a 'feature/*' branch."
fi
}
Usage is even simpler; just type the following when you're done with your feature:
gff
The function will extract the feature name from the branch and proceed with finishing it.
Setting Up Aliases for Efficiency
To avoid typing these commands out in full each time, we set up aliases in our Bash configuration:
alias gfs="git_flow_feature_start"
alias gff="git_flow_feature_finish"
Share your Feedback
If you find this useful, feel free to star my dotfiles
project on
GitHub. This is where I host all sorts of little hacks for my Bash/Git/Vim
environments.
If you tested these little helpers and have suggestions for improvement, feel free to open an issue or even fork the project.