How to Rename a Branch in Git Without Errors

Summarize this article with:

Typos happen. Project scopes change. That feature-login branch suddenly needs to be feature-auth.

Knowing how to rename a branch in Git saves you from deleting and recreating branches manually.

Git doesn’t have a direct rename command for remote branches. But with a few terminal commands, you can change branch names locally and remotely in under 5 minutes.

This guide walks you through 6 steps: checking your current branch, renaming local branches, pushing changes to your remote, and cleaning up stale references.

Works with Git Bash, Terminal, and any command line interface.

How to Rename a Branch in Git

maxresdefault How to Rename a Branch in Git Without Errors

Renaming a branch in Git is the process of changing an existing branch name using the git branch -m command in your local repository.

You need this when branch names contain typos, no longer reflect the feature scope, or don’t follow your team’s naming conventions.

This guide covers 6 steps requiring 2-5 minutes and basic familiarity with Git commands.

Prerequisites

Before you rename a branch, confirm you have:

Why is GitHub the heart of open source?

Uncover GitHub statistics: developer community growth, repository trends, collaboration patterns, and the platform that powers modern software development.

Explore GitHub Data →
  • Git version 2.0+ installed on your machine
  • Terminal or command line access
  • An initialized Git repository
  • Push access to the remote repository (for remote branch renaming)

Time estimate: 2 minutes for local branches, 5 minutes for remote.

Step One: How Do You Check Your Current Branch Name?

Run git branch in your terminal to display all local branches; the current branch shows an asterisk () next to its name, confirming which branch you’re working on before making changes.

Action

  1. Open your terminal in the repository directory
  2. Run: git branch
  3. Look for the asterisk () marking your current branch

To see remote branches too, use git branch -a instead.

Purpose

Verify which Git branch needs renaming before executing any commands.

Step Two: How Do You Rename the Local Branch You Are Currently On?

Use git branch -m new-branch-name while checked out on the branch you want to rename; the -m flag (short for –move) changes the branch pointer to the new name without affecting commit history.

Action

  1. Switch to the branch you want to rename
  2. Run: git branch -m new-branch-name
  3. No output means success

Replace new-branch-name with your desired name.

Purpose

This is the fastest method when you’re already working on the branch that needs a new name.

Step Three: How Do You Rename a Local Branch You Are Not On?

Run git branch -m old-branch-name new-branch-name from any branch to rename a different local branch; this approach lets you rename without switching your current working context.

Action

  1. Stay on your current branch (no checkout needed)
  2. Run: git branch -m old-branch-name new-branch-name
  3. Verify with: git branch -a

Purpose

Rename any branch in your local repository without interrupting your current workflow or staged changes.

Step Four: How Do You Delete the Old Branch Name From the Remote Repository?

Run git push origin --delete old-branch-name to remove the old branch reference from your remote; this command tells the origin server to delete the specified branch.

Action

  1. Run: git push origin --delete old-branch-name
  2. Wait for confirmation: [deleted] old-branch-name
  3. Replace “origin” if using a different remote name

Purpose

Remote branches cannot be renamed directly; you must delete the old name first.

Step Five: How Do You Push the Renamed Branch to the Remote Repository?

Execute git push origin -u new-branch-name to push your renamed branch and establish upstream tracking between your local and remote branches.

Action

  1. Run: git push origin -u new-branch-name
  2. The -u flag sets the upstream relationship
  3. Expected output: new-branch-name -> new-branch-name

Purpose

Creates the renamed branch on the remote server and links your local branch for future push and pull operations.

Step Six: How Do You Clean Up Stale Remote Tracking References?

Run git fetch --all --prune to sync your local tracking branches and remove references to branches that no longer exist on the remote server.

Action

  1. Run: git fetch --all --prune
  2. Alternative: git remote prune origin
  3. Stale references are removed automatically

The prune command cleans up outdated remote-tracking branches from your local clone.

Purpose

Keeps your local repository synchronized with the actual state of the remote.

Verification

Confirm the rename succeeded:

  1. Run git branch -a to list all branches
  2. New name appears; old name is gone
  3. Check GitHub, GitLab, or Bitbucket to verify remote changes

Troubleshooting

Branch Already Exists With New Name

Issue: Error message says branch name already taken.

Solution: Delete the conflicting branch first with git branch -D conflicting-branch, or pick a different name.

Permission Denied When Pushing

Issue: Remote server rejects your push request.

Solution: Verify you have write access to the repository; check SSH keys or authentication tokens.

Team Members Cannot Find the Branch

Issue: Collaborators still see the old branch name.

Solution: Team runs git fetch --all --prune, then checks out the new branch with git checkout -b new-branch-name origin/new-branch-name.

Old Branch Still Appears After Deletion

Issue: Stale reference persists in branch list.

Solution: Run git remote prune origin to force cleanup of outdated tracking references.

Related Processes

After renaming branches, you might need to:

Keep your codebase organized with consistent branch naming conventions across your version control workflow.

FAQ on How To Rename A Branch In Git

What is the command to rename a branch in Git?

Use git branch -m new-name to rename your current branch. To rename a different branch without switching, use git branch -m old-name new-name. The -m flag stands for “move,” which is how Git handles renaming.

Can I rename a remote branch directly?

No. Git doesn’t support direct remote branch renaming. You must rename the local branch first, delete the old remote branch with git push origin --delete old-name, then push the renamed branch to your remote.

Will renaming a branch affect commit history?

No. Renaming only changes the branch pointer reference. Your commit history stays intact. All commits, timestamps, and author information remain unchanged after the rename operation.

What happens to pull requests when I rename a branch?

Open pull requests linked to the old branch name may break. On GitHub, GitLab, or Bitbucket, you’ll need to close existing PRs and create new ones targeting the renamed branch.

How do I rename the main or master branch?

Same process applies. Rename locally with git branch -m main new-name, push to remote, then update your repository settings to change the default branch before deleting the old one.

What does the -M flag do versus -m?

The -M flag (uppercase) forces the rename even if the new branch name already exists. It overwrites the existing branch. The lowercase -m fails if a branch with the new name exists.

How do team members update their local copies after a rename?

Team members run git fetch --all --prune to sync changes. Then they checkout the new branch with git checkout new-branch-name or create a local tracking branch from origin.

Can I rename a branch while other developers are working on it?

Technically yes, but avoid it. Renaming active branches disrupts team workflows. Communicate with your team first. Coordinate the rename during low-activity periods to minimize conflicts.

Why does my old branch still appear after deletion?

Stale remote-tracking references linger locally. Run git remote prune origin or git fetch --prune to clean them up. This removes local references to branches deleted from the remote server.

Does renaming work the same in GitHub Desktop or other GUI tools?

Most source control GUI tools offer right-click rename options for local branches. Remote renaming still requires deleting and pushing. The underlying Git commands remain the same regardless of interface.

Conclusion

Learning how to rename a branch in Git takes minutes. The process stays the same whether you’re working solo or managing a shared repository.

Local renaming is one command. Remote renaming requires three: rename locally, delete the old remote branch, push the new one.

Clean up stale references with git fetch –prune`. Communicate changes to your team before renaming active branches.

Branch management is a core part of source control management. Consistent naming conventions keep your repository organized as projects scale.

Pair this with solid Git Flow practices and your dev and ops collaboration runs smoother.

Now go fix that typo in your branch name.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Rename a Branch in Git Without Errors
Related Posts