Git Branch Rename: change a branch name
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.
TE-89-A: branch -m renames the current branch and a named branch
Setup for this run, inside a throwaway directory (mktemp -d):
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 sideCommand:
git branch -m trunk; git branch -m side topic; git branch --format=%\(refname:short\) | sortRecorded output:
topic
trunkExit 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):
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 sideCommand:
git branch -M main; git branch --format='%(refname:short)'Recorded output:
mainExit 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):
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 sideCommand:
git branch -m side mainRecorded 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
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.
All full guides: All guides