devcrea

How to Rename a Git Branch: Local & Remote Guide for Developers

Learn how to rename a Git branch locally and remotely, handle CI/CD pipeline impact, fix case-sensitivity issues, and undo accidental renames. Step-by-step.

// The short answer

Rename the current Git branch with git branch -m new-name, then publish the new name with git push -u origin new-name and remove the old remote reference with git push origin --delete old-name when it is no longer needed. Update pull requests, branch protection, deployment files, and CI references before deleting the old branch.

Full walkthrough, edge cases and why it works
In this guide · 10 sections
  1. 01What Actually Happens Under the Hood?
  2. 02The -m vs. -M Trap (Watch Out for Letter Casing)
  3. 03Renaming a Branch Locally
  4. 04Updating the Remote Branch
  5. 05Quick sanity checks before you hit enter
  6. 06Dealing with Default Branches (main or master)
  7. 07Doing It via the Web UI (GitHub, GitLab, Bitbucket)
  8. 08Don't Let Your Pipelines Die Silently
  9. 09Getting the Rest of the Team Up to Speed
  10. 10Panic Button: How to Undo a Rename

Renaming a branch feels like the kind of low-stakes cleanup you do without thinking - right up until your build pipeline goes red because it can’t find the branch you just deleted. If you don't rewire the remote tracking properly, you end up silently snapping your automated deployments in half.

  • Rename the branch you’re on: git branch -m new-branch-name

  • Force a rename (fixing capitalization): git branch -M NEW-BRANCH-NAME

  • Update the remote: Wipe the old name off origin, then push the new one with -u.

  • What happens to your commits: Absolutely nothing (they stay right where they are).

  • CI/CD headache level: High (any workflow YAML pointing to the old name will choke).

What Actually Happens Under the Hood?

The good news is Git doesn't mess with your actual code or rewrite history when you do this. Branches aren't heavy folders; they’re just tiny, movable sticky notes pointing at a specific commit hash.

When you run a rename, Git simply takes that note, crosses out the old word, and writes the new one. All your file snapshots and commit IDs stay identical.

The -m vs. -M Trap (Watch Out for Letter Casing)

If you’re trying to change something subtle like feature-login to Feature-Login, you’ll likely hit a wall on macOS or Windows. Because their file systems ignore letter casing by default, Git trips over itself, insists that the branch already exists, and refuses to let you run a basic move command.

That’s where the capital -M comes in. It basically tells Git, "Yes, I know what I'm doing, overwrite the match." You won't hit this quirk on Linux boxes, but reaching for the capital flag just saves you the headache no matter what operating system you or your team run.

Renaming a Branch Locally

Renaming Whatever You Have Checked Out

Just hop onto the branch you want to touch up before you type the command. The -m here literally stands for move - which makes sense, given that Git is just moving a label across your commit tree.

Bash

code1 line
git branch -m new-branch-name

Renaming Another Local Branch Without Switching

You don’t actually have to interrupt your flow and checkout a branch just to tweak its label. You can pass the original label and the target label together from wherever you happen to be sitting:

Bash

code1 line
git branch -m old-branch-name new-branch-name

It’s a handy little shortcut when you're tidying up a backlog of messy branch names and don't feel like constantly swapping your working tree around.

Updating the Remote Branch

Changing things on your laptop doesn't automatically mirror over to GitHub or GitLab. You have to push the fresh name up and manually ditch the old reference that's lingering on the server.

Step 1: Blow Away the Old Remote Branch

Bash

code1 line
git push origin --delete old-branch-name

This clears out the stale record on the server so nobody else on your team accidentally pulls down an abandoned reference the next time they run a fetch.

Step 2: Push the Fresh Branch and Lock In Tracking

Bash

code1 line
git push origin -u new-branch-name

Throwing in that -u flag is crucial because it wires up the upstream link. Once that's set, plain old git pull and git push will work automatically without you having to spell out origin every single time. You can double-check that the relationship stuck with:

Bash

code1 line
git branch -vv

If the terminal still shows you linked to the ghost of the old branch, just hook it up by hand:

Bash

code1 line
git branch --set-upstream-to=origin/new-branch-name

Quick sanity checks before you hit enter

Jumping into a rename halfway through a sprint without glancing around usually creates way more fires than a tidy branch name is worth. Take ten seconds to check these off:

  • Any pull requests still floating out there? GitHub is smart enough to redirect open PRs automatically when you rename. GitLab and Bitbucket aren't always so forgiving, and you might have to manually repoint the target branch.

  • Is this the default branch? If you’re touching main or master, stop here - you’ll need repository admin permissions and a couple of platform settings tweaks (walked through below).

  • Is anyone actively hacking on this with you? Give them a quick heads-up before you delete anything remote. Their local clones will cling to the dead name until they run a prune.

  • Did anyone hardcode this name in CI configs? Run git grep "old-branch-name" -- "*.yml" across your tree before you pull the trigger.

Dealing with Default Branches (main or master)

Messing with the root branch of a project isn't just another terminal command; you have to treat it with a bit of care. Before you start pushing new branch labels, you need to head into your repository’s web settings and officially switch the default branch pointer there first. Make sure you actually hold owner or admin permissions on the repo, too, because regular write access won't let you swap out the root.

Doing It via the Web UI (GitHub, GitLab, Bitbucket)

If you're on GitHub, you can skip the terminal dance entirely by using the rename pencil right inside the web interface. Doing it that way is great because GitHub automatically handles repointing any active Pull Requests that were aimed at the old branch. Just keep in mind that GitLab and Bitbucket can be a little clunkier, so expect to manually fix your merge requests there after making the switch.

Don't Let Your Pipelines Die Silently

Any build script or automation runner that relies on hardcoded branch strings is going to choke the second that branch name disappears. CI/CD runners aren't intuitive - they look for an exact, case-sensitive match, and if it's gone, they just fail to run.

Check your workflows inside .github/workflows or your .gitlab-ci.yml files for any lingering ghosts:

Bash

code1 line
git grep "old-branch-name" -- "*.yml"

Make sure your on: push: branches: rules match your shiny new name. If you skip this, you’ll push your latest work and wonder why your test suite or staging deploys never kicked off. And while you're in there, scan through any build flags or environment scripts that might be checking branch environment variables.

Getting the Rest of the Team Up to Speed

Once you've pushed the new branch, anyone else who had the old version checked out will need to run a small clean-up sequence locally to get on the same page.

Bash

code11 lines
# Step 1: Grab the fresh remote state and dump dead branches
git fetch --prune

# Step 2: Rename the local copy to match
git branch -m old-branch-name new-branch-name

# Step 3: Wire up tracking to the shiny new upstream
git branch --set-upstream-to=origin/new-branch-name

# Step 4: Scrap the leftover old branch locally if it's hanging around
git branch -d old-branch-name

Don't skip that fourth step - it gets rid of the redundant local branch so folks don't end up with duplicate, confusing entries staring at them in git branch. Passing --prune in that first fetch also sweeps away all the stale remote-tracking tags, meaning that deleted remote branch won't keep popping up like a zombie.

Panic Button: How to Undo a Rename

If you realize you made a typo or changed the wrong branch entirely, don't sweat it. Git keeps a rolling diary of everywhere your HEAD pointer has been through the reflog:

Bash

code1 line
git reflog

Just look for the commit hash right before you pulled the trigger on the rename, check it out, and recreate your old branch name from there. It's nearly impossible to lose actual work this way.

And if you already pushed the mistake upstream, simply push your recreated branch back to origin and wipe out the accidental one using the remote commands we walked through earlier.

Quick answers

Frequently asked questions

What is the difference between git branch -m and -M?

Both flags rename a branch, but -M forces the rename even when the new name conflicts on case-insensitive file systems (Windows and macOS). If you rename feature-login to Feature-Login on those systems, -m throws a fatal error because Git sees the name as already taken. Use -M to override that check.

Can you rename a git branch without checking it out?

Yes. Run git branch -m old-branch-name new-branch-name from any branch. You only need to be on the branch if you omit the old name: git branch -m new-branch-name renames whichever branch is currently checked out.

What happens to commits when you rename a git branch?

Nothing changes. Commits are identified by their SHA hashes, not by branch names. Renaming a branch just moves a lightweight pointer to a new label. Your entire commit history, file contents, and timestamps remain identical.

How do you rename a remote git branch?

Git has no direct remote rename command. You delete the old remote branch with git push origin --delete old-name, then push the renamed local branch with git push origin -u new-name. The -u flag sets upstream tracking so future push and pull commands work without specifying the remote.

How do you undo a git branch rename?

Run git reflog to find the entry from before the rename, then rename it back with git branch -m current-wrong-name original-name. If you already deleted the old remote branch, push it back with git push origin -u original-name and delete the new remote name.

Does renaming a branch affect open pull requests?

On GitHub, renaming via the web UI automatically updates open pull requests targeting that branch. On GitLab and Bitbucket, you usually need to manually re-target affected merge requests after renaming. Always check open PRs after any remote branch rename.

Does renaming a branch break CI/CD pipelines?

Yes, if your pipeline config files reference the old branch name as a hardcoded string. Search your .github/workflows or .gitlab-ci.yml files for the old name and update any on: push: branches: triggers before or immediately after pushing the renamed branch.

Something wrong or out of date?

Tools change fast. Tell the editor what no longer works.