When working with Git, you often want to create a new feature branch based on a common base branch such as develop. Doing this repeatedly with full Git commands can get tedious, so letβs streamline it using a custom Zsh function.
The manual way
Normally, to create and switch to a new branch based on develop, you would run:
git switch -c feature/login develop
This tells Git to:
- Create a new branch called
feature/login - Base it on
develop - Switch to it immediately
If you do this several times a day, it quickly becomes repetitive. Letβs fix that.
Adding a custom command in Zsh
The cleanest way to automate this is by adding a function to your ~/.zshrc file. Functions handle parameters more gracefully than aliases, which makes them perfect for this use case.
Open your Zsh configuration:
nano ~/.zshrc
Then add the following function:
gbd() {
if [ -z "$1" ]; then
echo "Usage: gbd <new-branch-name>"
return 1
fi
git switch -c "$1" develop
}
Save the file and reload your shell:
source ~/.zshrc
Now you can run:
gbd feature/login
This will create a new branch feature/login based on develop and switch to it immediately.
Keeping develop up to date automatically
If you want to make sure your develop branch is always up to date before branching off, you can enhance the function like this:
gbd() {
if [ -z "$1" ]; then
echo "Usage: gbd <new-branch-name>"
return 1
fi
git fetch origin develop:develop && git switch -c "$1" develop
}
This fetches the latest version of develop from the remote repository before creating your new branch.
Optional: add naming conventions
If your team uses naming conventions such as feature/, fix/, or hotfix/, you can add a simple prefix automatically:
gbd() {
if [ -z "$1" ]; then
echo "Usage: gbd <type>/<name> (e.g. feature/login)"
return 1
fi
git fetch origin develop:develop && git switch -c "$1" develop
}
Now you can run:
gbd feature/login
and your workflow stays consistent across projects.
Conclusion
With a simple function in your ~/.zshrc, you can create new branches from develop in one short command:
gbd new-branch-name
This small change eliminates repetitive typing, keeps your workflow consistent, and ensures you always start from the correct base branch.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.