theme: ricetheme
New palette
New palette

Rename Git Branch Locally and on Your Remote

Rename a Git branch locally and remotely, then update collaborators, upstream tracking, and dotfiles links so old names stop breaking your scripts.

Terminal interface showing git branch rename from old-login to new-login, with sync arrows and cloud server confirmation.

tl;dr the short answer

Renaming a Git branch changes its name without changing its commits, but a local rename does not update the remote or other people’s copies. Rename it locally, push the new name and set its upstream, then delete the old remote branch; check pull requests, branch rules, scripts, and links that still use the old name. Tell collaborators to fetch and re-track the branch so everyone can keep working with the same name.

Renaming a file on your desktop takes two seconds. Renaming a folder your friends already have bookmarked takes more thought, because everyone relying on the old name has to hear about it. Git branches work the same way.

A branch is simply a named line of work, and renaming one on your own machine is quick. The trouble starts when the remote and your collaborators still use the old name.

In this post, we’ll cover renaming a branch locally and on the remote, updating your default branch, re-syncing collaborators, and fixing upstream tracking.

We’ll finish with your dotfiles repo, the place many of us keep personal config files, and the details that break bootstrap scripts and raw file links when a rename slips through.

Git Rename Branch Workflow at a Glance

Renaming a branch touches two places: your local copy and the remote your team pulls from. Here’s the full workflow up front.

Two methods, shown step by step

Both methods end with the same names everywhere. They differ in when the old name disappears.

Method one: rename first, then swap the remote. Use this when you’re already on the branch.

  1. Rename the local branch:

    git branch -m new-name
    
  2. Push the new name and set its upstream, the remote branch your local one tracks:

    git push -u origin new-name
    
  3. Delete the old name on the remote:

    git push origin --delete old-name
    

Method two: push first, then clean up. Use this when you want the old name live until the swap is done.

  1. Push your local branch under the new remote name:

    git push origin old-name:new-name
    
  2. Rename it locally, even if you’re not on it:

    git branch -m old-name new-name
    
  3. Point the local branch at its new upstream:

    git branch -u origin/new-name new-name
    
  4. Remove the old remote branch:

    git push origin --delete old-name
    

Why branch names matter and further reading

Branch names are easy to pick and surprisingly sticky. The rename itself takes seconds, but the references to the old name are what take the effort.

Consider a Linux user who keeps dotfiles in git and bootstraps each fresh install with a one-liner that downloads a setup script from a raw URL on a branch called feature/waybar-rice.

Rename that branch later, and the one-liner fails on the new laptop they’re setting up at 1am. A quick search for the old name would have caught it. Search first. Rename second.

This post is one piece of a broader git tutorial, so the basics elsewhere in that series will help you here. For deeper dives on any command above, a good git reference is your next stop.

Sleepy developer sees a 404 error from a renamed git branch, with a magnifying glass highlighting the need to search first.

Changing the Branch Name on Your Computer

Renaming a branch on your own machine takes one command, and it’s the step to finish before anything touches the remote. Here’s how the command works, and when you can skip switching branches first.

Using -m to move a branch name

Git handles renames with the -m flag, which means move. Moving a branch changes its name, not its commits.

Git gives you two forms:

  • New name only: git branch -m new-name renames the branch you’re on.
  • Old and new name: git branch -m old-name new-name renames the branch you specify.

The old-and-new form covers the classic case of renaming master to main: git branch -m master main. Consider a Fedora user whose dotfiles repo still uses master and who wants main everywhere.

One command handles the local side, and the commit history stays intact.

To stop new repos from starting on master, set init.defaultBranch in your ~/.gitconfig with git config --global init.defaultBranch main. Set it once, and every repo you create afterward starts on main.

Whether to switch branches before renaming

You don’t always have to switch first. The two-name form lets you rename any branch while you’re on a different one, so you can fix a name without leaving your current work.

Think of a tmux-and-ssh homelabber partway through a change on main who spots a branch called vpn-tunel. Running git branch -m vpn-tunel vpn-tunnel renames it from where they stand, and their uncommitted edits stay put.

If you’d rather switch first, check out the branch you want to rename with git checkout and use the one-name form. The one-name form works on the branch you’re on, but the two-name form reaches one you’re not on. Your commits stay put.

A flat illustration shows a developer renaming a git branch while uncommitted files remain safe, featuring terminal windows and server icons.

Sending the Rename to Your Remote

A local rename only changes your copy, so origin (your remote’s default name) still shows the old name until you push.

Removing the old branch name from origin

Check open pull requests (PRs) aimed at the old name first, and update any continuous integration (CI) config or branch protection rules that mention it. Once the new name is up, you’ll delete the old one with git push origin --delete old-name.

The colon syntax, git push origin :old-name, does the same thing.

Consider a student who renamed wip-bar to waybar-theme and left the old branch behind, so the remote shows both names.

Push the new name and set upstream

After the rename, push the new name with git push -u origin new-name. The -u flag sets the upstream, the link between your local branch and its remote partner. Set it once, not on every push, and plain git push handles the rest.

For example, a waybar tinkerer pushes bar-colors with -u, then types only git push from then on.

Dotfiles repos, which hold your config files, feel this hardest. Raw URLs (web addresses that serve one file straight from a branch) pin the branch name in their path. Think of an Arch user whose one-line bootstrap curls install.sh from the old branch.

After the rename, that command fails, and the new machine never gets its hyprland config. Update install scripts and README references to match. Your scripts won’t fix themselves.

Restoring upstream tracking after a rename

A rename can also break tracking. Once the old remote branch is gone, git will warn you that your upstream is gone. Clear the dead link with git branch --unset-upstream, then reset it with git push -u origin new-name.

Teammates need the same fix: git fetch --prune clears stale copies of the old name, and git branch -u origin/new-name re-tracks the branch. Consider a self-hoster sharing a repo with a friend.

Their next pull may complain until they re-track, so a quick heads-up saves confusion.

Other Git and Terminal Commands Worth Knowing

Here’s the everyday toolkit you’ll reach for in almost every cleanup or rename job.

List, create and switch between branches

To see every branch, run git branch, which marks the current one with an asterisk. Add -a to also list the remote branches, the copies stored on the server.

git branch new-name creates a branch without moving you onto it, while git switch -c new-name creates it and switches you there in one step.

To move between branches, use git switch branch-name. You’ll also see git checkout, which can switch too but also restores files, so git switch only changes branches and is the safer habit.

Picture a tinkerer testing a waybar-test branch, then switching back the moment the bar breaks.

A developer switches from a buggy git branch to main, fixing their status bar interface.

How to delete a branch locally and remotely?

A branch can live in two places, so deleting it takes two commands:

  • Local: git branch -d old-branch
  • Remote: git push origin --delete old-branch

The lowercase -d is the safe option: it refuses to delete a branch with unmerged work. Uppercase -D forces the deletion, so save it for branches you’re certain you don’t need.

Afterward, run git branch -a to confirm both copies are gone.

Moving between folders in the terminal

cd changes directory: cd ~/dotfiles drops you into that folder, and cd .. moves you up one level. Think of a ricer whose configs live in ~/dotfiles. One cd puts them inside the repo before any git command runs.

ls lists what’s in the folder you’re standing in. The asterisk is a wildcard, a stand-in for any filename, so ls *.conf shows only the files ending in .conf.

To confirm you’re in the right place, run ls -la. The -a flag reveals hidden files, including the .git folder that marks a real repository.

Renaming files, folders and whole projects

The mv (move) command does double duty: mv notes.txt todo.txt renames a file in place, and mv old-folder new-folder renames a folder. A project is just a folder, so renaming one works the same way.

Consider a student who renamed school-stuff to thinkpad-setup and wondered why the hosted repo kept the old name. Folder and repo names are separate, and renaming the folder changes your local copy, not the hosted one.

Rename the repo on the server, then update your remote with git remote set-url origin and the new address.

rm * deletes every matching file in the current folder. Hidden dotfiles like .bashrc survive, but everything else goes, and rm doesn’t use a trash folder. Deletion is permanent. Check with ls first.

A branch rename looks small, but it has a long tail. Your local copy takes one command, while the remote, your collaborators, and your dotfiles all depend on the old name, so each one needs its own check.

Work through them in order and nothing breaks quietly in the background.

Your repository should reflect how you actually work. Once the remote, your team, and your scripts agree on the new name, you’re free to move on. Rename it once, check everything, and get back to building.

Questions

How do I switch between branches?

Use `git switch branch-name` to move to an existing branch. You can also use `git checkout branch-name`, but `git switch` only changes branches.

How to delete git branch both local and remote?

Delete the local branch with `git branch -d branch-name`, then delete the remote branch with `git push origin --delete branch-name`. The `-d` option refuses to remove a branch with unmerged work; use `-D` only when you intend to force deletion.

How do I list all branches in git?

Run `git branch` to list local branches; the current branch has an asterisk. Run `git branch -a` to include remote branches.

How do I create a git branch?

Run `git branch new-name` to create a branch without switching to it. To create the branch and switch to it, run `git switch -c new-name`.

How do I rename a project?

A project is a folder, so rename its local folder with `mv old-name new-name`. To rename the hosted repository too, change its name on the hosting service and update your local remote URL with `git remote set-url origin new-address`.

How do I rename a folder name?

Use `mv old-folder new-folder` to rename a folder in the terminal. This changes the folder name in your local copy.

How to rename a file in terminal command?

Use `mv old-name new-name` to rename a file in the terminal, such as `mv notes.txt todo.txt`.

Can the mv command rename files?

Yes. The `mv` command can rename a file when you move it to a new name in the same folder, as in `mv notes.txt todo.txt`. It can also rename folders.