BASH

Git: Create and Push New Feature Branch from Development

A Bash script to automate the common Git workflow of creating a new feature branch from the 'development' branch, switching to it, and pushing it to the remote repository.

#!/bin/bash

if [ -z "$1" ]; then
  echo "Usage: $0 <feature-branch-name>"
  exit 1
fi

FEATURE_BRANCH="feature/$1"
DEVELOPMENT_BRANCH="development"

echo "Ensuring we are on $DEVELOPMENT_BRANCH and up-to-date..."
git checkout $DEVELOPMENT_BRANCH && git pull origin $DEVELOPMENT_BRANCH

if [ $? -ne 0 ]; then
  echo "Error: Could not update $DEVELOPMENT_BRANCH. Aborting."
  exit 1
fi

echo "Creating new branch: $FEATURE_BRANCH"
git checkout -b $FEATURE_BRANCH

if [ $? -ne 0 ]; then
  echo "Error: Could not create branch $FEATURE_BRANCH. Aborting."
  exit 1
fi

echo "Pushing new branch to origin..."
git push -u origin $FEATURE_BRANCH

if [ $? -ne 0 ]; then
  echo "Error: Could not push branch $FEATURE_BRANCH to origin."
  exit 1
fi

echo "Branch $FEATURE_BRANCH created and pushed successfully!"
How it works: This script automates a common Git workflow: ensuring the `development` branch is up-to-date, creating a new `feature/` branch from it, switching to the new branch, and then pushing it to the remote repository. It includes basic error checking to ensure each Git command succeeds before proceeding. This streamlines the process for developers, reducing manual steps and potential errors in setting up new feature work.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs