日本語

How to Rename and Delete Git Branches

​​Git​command line

Use git branch -m to fix a misspelled branch name, and git branch -d to clean up a branch that is merged and no longer needed. -d refuses to delete a branch that is not merged, while -D deletes it regardless of merge status. A branch that has been pushed is not renamed or deleted on the remote when you do it locally, so you need a separate git push for that.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables so the output stays the same each time. The working folder was named /path/to, so that is the path that appears in the output. The remote was a bare repository (../remote.git) created in the same environment. I did not test operations on hosting services such as GitHub (for example, changing the default branch), and I did not test Windows or macOS.

The repository I tested

The repository has main and three other branches. git branch -vv shows the latest commit of each branch, and the remote branch each one tracks (its upstream) appears in square brackets (official reference for git branch).

console
$ git branch -vv
  experiment    ebb517a キャッシュを試す
  feature/lgoin a5cc578 [origin/feature/lgoin] ログイン画面を追加
  fix-typo      72ea9ab READMEの誤字を直す
* main          aaab4fc [origin/main] fix-typo をマージ

feature/lgoin is a branch I pushed under the misspelled name lgoin instead of login. fix-typo is already merged into main, and experiment was created for a trial and is not merged.

Rename a branch with git branch -m

Rename a branch other than the current one

Use git branch -m 現在の名前 新しい名前 (old name, new name). While still on main, I renamed feature/lgoin to feature/login. On success, it prints nothing.

console
$ git branch -m feature/lgoin feature/login

$ git branch -vv
  experiment    ebb517a キャッシュを試す
  feature/login a5cc578 [origin/feature/lgoin] ログイン画面を追加
  fix-typo      72ea9ab READMEの誤字を直す
* main          aaab4fc [origin/main] fix-typo をマージ

The local branch name changed, but its upstream is still origin/feature/lgoin. The official reference says -m also moves the branch's configuration and reflog. The upstream setting did move to the new branch, still with the old name.

console
$ git config --get-regexp "^branch\\.feature"
branch.feature/login.remote origin
branch.feature/login.merge refs/heads/feature/lgoin

Rename the branch on the remote as well

To rename the remote branch, push under the new name and then delete the old name from the remote (official reference for git push). Pushing with -u also switches the upstream to the new name.

console
$ git push -u origin feature/login
To ../remote.git
 * [new branch]      feature/login -> feature/login
branch 'feature/login' set up to track 'origin/feature/login'.

$ git push origin --delete feature/lgoin
To ../remote.git
 - [deleted]         feature/lgoin

$ git branch -vv
  experiment    ebb517a キャッシュを試す
  feature/login a5cc578 [origin/feature/login] ログイン画面を追加
  fix-typo      72ea9ab READMEの誤字を直す
* main          aaab4fc [origin/main] fix-typo をマージ

$ git ls-remote --heads origin
a5cc578bae648ee74cc7f9aa1d735203cb8fce69	refs/heads/feature/login
aaab4fc73e6d34b88b0e473c85bb4733ea697786	refs/heads/main

The remote now has two branches, feature/login and main.

Rename the current branch

Leave out the old name and use git branch -m 新しい名前, and the branch you are on is renamed. I switched to experiment and renamed it to try-cache.

console
$ git switch -q experiment

$ git branch
* experiment
  feature/login
  fix-typo
  main

$ git branch -m try-cache

$ git branch
  feature/login
  fix-typo
  main
* try-cache

$ git status | head -1
On branch try-cache

You cannot rename to a name that already exists

Trying to rename a branch to the name of another branch stops with an error.

console
$ git branch -m main
fatal: a branch named 'main' already exists

I tested -M, which renames to a name that already exists, at the end of this article.

Delete a branch with git branch -d

Delete a merged branch

Before deleting, it is safer to check which branches are merged into the current branch (HEAD) with git branch --merged. Adding --no-merged lists the branches that are not merged yet.

console
$ git switch -q main

$ git branch --merged
  fix-typo
* main

$ git branch --no-merged
  feature/login
  try-cache

$ git branch -d fix-typo
Deleted branch fix-typo (was 72ea9ab).

fix-typo is deleted. The commit the deleted branch last pointed to appears after was.

-d cannot delete a branch that is not merged

If you try to delete try-cache, which is not merged into main, with -d, it is refused. The error message says to use -D if you really want to delete it.

console
$ git branch -d try-cache
error: the branch 'try-cache' is not fully merged.
If you are sure you want to delete it, run 'git branch -D try-cache'

$ git branch -D try-cache
Deleted branch try-cache (was ebb517a).

$ git branch
  feature/login
* main

The official reference describes -D as a short form of --delete --force, which deletes the branch regardless of whether it is merged.

Recreate a branch you deleted by mistake

Even after deleting with -D, you can recreate a branch pointing to the same commit by passing the hash shown after was.

console
$ git branch try-cache ebb517a

$ git log --oneline -1 try-cache
ebb517a キャッシュを試す

According to the official reference, deleting a branch also deletes its reflog. The hash of the deleted branch is shown after was when you run -D.

You cannot delete the current branch

You cannot delete the branch you are on, even with -D.

console
$ git switch -q try-cache

$ git branch -D try-cache
error: cannot delete branch 'try-cache' used by worktree at '/path/to/app'

$ git switch -q main

$ git branch -D try-cache
Deleted branch try-cache (was ebb517a).

After switching to another branch, I was able to delete it.

With an upstream branch, the check that -d performs changes

The check -d uses to decide whether a branch is merged is not always against HEAD. The official reference says that if the branch has an upstream, it must be merged into that upstream; otherwise it must be merged into HEAD.

feature/login is not merged into main, but it has been pushed to its upstream, origin/feature/login. While on main, I ran -d on it. It printed a warning, but the branch was deleted.

console
$ git branch --no-merged
  feature/login

$ git branch -d feature/login
warning: deleting branch 'feature/login' that has been merged to
         'refs/remotes/origin/feature/login', but not yet merged to HEAD
Deleted branch feature/login (was a5cc578).

The commit still exists on the remote, so when I specified the branch name with git switch, the local branch was recreated from origin/feature/login.

console
$ git switch feature/login
Switched to a new branch 'feature/login'
branch 'feature/login' set up to track 'origin/feature/login'.

$ git switch -q main

Delete a branch on the remote

Delete a remote branch with git push origin --delete ブランチ名. After that, git branch -vv showed gone on the upstream of the local feature/login.

console
$ git push origin --delete feature/login
To ../remote.git
 - [deleted]         feature/login

$ git branch -r
  origin/main

$ git branch -vv
  feature/login a5cc578 [origin/feature/login: gone] ログイン画面を追加
* main          aaab4fc [origin/main] fix-typo をマージ

Deleting the remote branch leaves the local branch in place. After its upstream was gone, -d refused to delete it, because it has commits that are not merged into main. If you do not need the branch, you can delete it with -D.

console
$ git branch -d feature/login
error: the branch 'feature/login' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature/login'

$ git branch -D feature/login
Deleted branch feature/login (was a5cc578).

Tell other clones with git fetch –prune

Before deleting the remote branch, I also cloned the repository into another folder (../clone). There, a plain git fetch left origin/feature/login in place.

console
$ git -C ../clone fetch

$ git -C ../clone branch -r
  origin/HEAD -> origin/main
  origin/feature/login
  origin/main

With --prune, the origin/… branches that point to branches no longer on the remote (remote-tracking branches) are removed locally too (official reference for git fetch).

console
$ git -C ../clone fetch --prune
From /path/to/app/../remote
 - [deleted]         (none)     -> origin/feature/login

$ git -C ../clone branch -r
  origin/HEAD -> origin/main
  origin/main

Overwrite an existing branch with -M

According to the official reference, -M (the same as -m --force) renames a branch even when the new name already exists. After cleaning up the other branches, I created a new keep that points to aaab4fc, the same as main, and other that points to ebb517a, and tested it.

console
$ git branch -v
  keep  aaab4fc fix-typo をマージ
* main  aaab4fc fix-typo をマージ
  other ebb517a キャッシュを試す

$ git branch -m other keep
fatal: a branch named 'keep' already exists

$ git branch -M other keep

$ git branch -v
  keep ebb517a キャッシュを試す
* main aaab4fc fix-typo をマージ
Warning

Renaming to an existing name with -M deleted the original keep without asking, and keep now points to ebb517a, which other used to point to.

​Posted by 86