Git | ブランチ名を変更する

Git でブランチを新規作成する際にうっかりタイポしてしまったので、別に正しい名前のブランチを作ってもいいが、ブランチ名を変更できないかなと思い調べてみた。

ブランチ名を変更するには、おそらく branch コマンドで remove をキーワードに調べてみればいいはず。

1
2
3
4
5
6
$ git branch --help | grep move
its merged status. In combination with -m (or --move), allow renaming the branch even if the new branch
-m, --move
Shortcut for --move --force.
Remove the upstream information for <branchname>. If no branch is specified it defaults to the current
`

見つけた!これで変更できそう。

1
git branch -m <古いブランチ名> <新しいブランチ名>

動作確認

  1. 間違ったブランチを作成する

    1
    $ git branch WrongBranch
  2. ブランチができていることを確認する

    1
    2
    3
    $ git branch
    WrongBranch
    * main
  3. ブランチ名を変更する

    1
    $ git branch -m WrongBranch CorrectBranch
  4. 修正できたことを確認する

    1
    2
    3
    $ git branch
    CorrectBranch
    * main

ブランチ名を変更できた。