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 <branch-name>

PARAMETERS

<branch-name>
    Required positional argument specifying the name of the new branch to create from the latest base branch

DESCRIPTION

The git-fresh-branch command is a popular shell function or alias used by developers to streamline Git workflows. It automates the process of creating a new feature branch based on the most recent commits from the project's main or master branch, reducing the risk of integration issues from stale local branches.

Typically, it performs these steps:
1. Switches to the base branch (e.g., main or master).
2. Fetches and pulls the latest changes from the remote repository.
3. Creates and switches to a new branch with the specified name.

This is especially useful in trunk-based development, CI/CD pipelines, and team environments where branches must start from the tip of main to minimize merge conflicts. It ensures developers work on up-to-date code without manual steps.

Implementations vary slightly; some delete an existing local branch with the same name before creating the new one. It's not part of core Git but is widely shared in dotfiles, blogs, and tools like git-extras or custom scripts.

CAVEATS

Not a built-in Git command; must be defined as a shell function or alias in ~/.bashrc, ~/.zshrc, etc. Assumes base branch is named 'main' or 'master'—customize script if different (e.g., 'develop'). May fail if remote fetch/pull encounters issues like authentication or conflicts.

TYPICAL IMPLEMENTATION

Add to shell profile:
git-fresh-branch() {
  git checkout main &&
  git pull origin main &&
  git checkout -b $1
}

Reload shell or source ~/.bashrc.

EXAMPLE USAGE

git-fresh-branch feature/login

This fetches latest main, then creates and switches to feature/login.

HISTORY

Emerged in the mid-2010s amid rise of trunk-based development and Git best practices. Popularized via developer blogs (e.g., Thoughtbot, GitHub gists), dotfiles repositories, and tools like bash-it or oh-my-zsh plugins. Evolved from manual sequences like 'git co main; git pull; git co -b newfeat' to promote efficient, conflict-free branching.

SEE ALSO

git branch(1), git checkout(1), git pull(1), git fetch(1)

Copied to clipboard