# Git Branch Rename: change a branch name

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-branch-rename-full-en
`git branch -m NEW` renames the current branch. `git branch -m OLD NEW` renames another branch. Only branches without a remote were renamed.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-89-A, TE-89-B and TE-89-C. The expected non-zero exit is TE-89-C (exit status 128). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-89-A: branch -m renames the current branch and a named branch

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
git branch side
```

Command:

```bash
git branch -m trunk; git branch -m side topic; git branch --format=%\(refname:short\) | sort
```

Recorded output:

```
topic
trunk
```

Exit status: 0. The output matches the expected text written before the run. This is the status of sort, the last command in the pipe.

### TE-89-B: git branch -M NEW renames the current branch over an existing branch name

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
git branch side
git switch -q side
```

Command:

```bash
git branch -M main; git branch --format='%(refname:short)'
```

Recorded output:

```
main
```

Exit status: 0. This status matches the corrected expectation, written after a first run that did not match. The first prediction used git branch -M side main while main was checked out. Git refused with fatal: Cannot force update the current branch. The test was changed to rename the checked-out branch, and all tests were run again.

### TE-89-C: expected non-zero exit: git branch -m onto a branch name that already exists stops with a fatal error

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
git branch side
```

Command:

```bash
git branch -m side main
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
fatal: A branch named 'main' already exists.
```

Exit status: 128. This status and the output match what was written before the run.

TE-89-B: by the setup, side is the checked-out branch and main also exists. git branch -M main renamed side to main over the old main, and the branch list shows only main. TE-89-C: git branch -m side main exited with status 128, and standard error reads fatal: A branch named 'main' already exists.

## Limits

Only branches without a remote were renamed. Renaming a branch that is pushed needs more steps on the remote, which were not tested.

## Source

- https://git-scm.com/docs/git-branch

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-89-A` | branch -m renames the current branch and a named branch | 0 | PASS | `52abda393821` |
| `TE-89-B` | git branch -M NEW renames the current branch over an existing branch name | 0 | PASS | `90157de095c7` |
| `TE-89-C` | expected non-zero exit: git branch -m onto a branch name that already exists stops with a fatal error | 128 | PASS | `79744ca025ed` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Git Ls Files: list what Git tracks](https://kairos-2z6s.onrender.com/res/git-ls-files-full-en)
- [Git Rev Parse: ask Git for a path or a commit id](https://kairos-2z6s.onrender.com/res/git-rev-parse-full-en)
- [git clean: delete untracked files, with a dry run first](https://kairos-2z6s.onrender.com/res/git-clean-full-en)
- [Git Branch Delete: -d versus -D](https://kairos-2z6s.onrender.com/res/git-branch-delete-full-en)
- [git bisect: find the commit that broke something](https://kairos-2z6s.onrender.com/res/git-bisect-full-en)
