LinuxCommandLibrary

git-fresh-branch

Create a clean branch from remote

TLDR

Create an empty local branch

$ git fresh-branch [branch_name]
copy

SYNOPSIS

git fresh-branch [OPTIONS] <new-branch-name> [<base-branch-name>]

PARAMETERS

<new-branch-name>
    The desired name for the new branch to be created. This is a mandatory argument.

<base-branch-name>
    The existing branch from which to create the new branch (e.g., main, master, develop). If not specified, the command typically defaults to a common base branch (often main or master) after pulling updates.

-n, --no-push
    Prevents the command from automatically pushing the newly created branch to the remote repository. The branch will only be created locally.

DESCRIPTION

git-fresh-branch is a convenient wrapper script for Git that streamlines the process of creating a new branch. It typically starts by ensuring your base branch (e.g., main or master) is up-to-date by pulling the latest changes. It then creates and checks out a new branch from this refreshed base. Optionally, it can immediately push the newly created branch to the remote repository, setting it up for tracking.

This command saves time and reduces common errors by automating several standard Git operations into a single, intuitive command. It's especially useful for maintaining a clean branch history and quickly starting new features or bug fixes.

CAVEATS

This is typically a community-contributed script and not a built-in Git command. Its availability and exact behavior depend on whether it's installed on your system. The default base-branch-name (e.g., main vs. master) might vary based on the script's specific implementation or your Git configuration. It generally assumes you have push access to the remote repository if you're not using --no-push.

AUTOMATIC BASE BRANCH UPDATE

Before creating the new branch, git-fresh-branch often automatically fetches and pulls the latest changes for the specified or default base branch (e.g., main), ensuring your new branch is always up-to-date from its origin.

REMOTE TRACKING SETUP

When pushing the new branch (unless --no-push is used), the command typically sets up the upstream tracking for the new branch, meaning future git pull or git push commands on that branch will work seamlessly without specifying the remote or branch name.

HISTORY

git-fresh-branch (or similar utility scripts like git-new-branch) emerged from the Git community's desire to simplify common multi-step workflows. Developers often found themselves repeatedly performing git checkout main, git pull, git checkout -b new-branch, and git push -u origin new-branch. Scripts like this automate these repetitive steps, improving efficiency and reducing human error. Its development is generally grassroots, driven by individual developers sharing useful shortcuts.

SEE ALSO

Copied to clipboard