リモートブランチをリネームする

GitGitBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

Git では、ブランチはコミットへの軽量の可動ポインタです。ブランチをリネームすることは Git で一般的なタスクです。ただし、ローカルおよびリモートでブランチをリネームすることは少々面倒になる場合があります。この実験では、Git でリモートブランチをリネームする方法を学びます。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/CollaborationandSharingGroup(["Collaboration and Sharing"]) git/CollaborationandSharingGroup -.-> git/remote("Manage Remotes") subgraph Lab Skills git/remote -.-> lab-12752{{"リモートブランチをリネームする"}} end

リモートブランチをリネームする

この実験を完了するには、GitHub アカウントから Git リポジトリ git-playground を使用します。これは https://github.com/labex-labs/git-playground.git のフォークから来ています。フォークする際は、「マスターブランチのみをコピーする」をオフにしてください。

https://github.com/your-username/git-playground という名前の Git リポジトリがあります。feature-branch という名前のブランチを作成し、リモートにプッシュしました。今では、ローカルおよびリモートでブランチ名を new-feature-1 に変更したいと思っています。

  1. リポジトリをクローンし、ディレクトリに移動して ID を設定します。
    git clone https://github.com/your-username/git-playground.git
    cd git-playground
    git config --global user.name "your-username"
    git config --global user.email "your-email"
  2. feature-branch という名前のブランチに切り替えます。
    git checkout feature-branch
  3. ローカルおよびリモートでブランチをリネームします。
    git branch -m feature-branch new-feature-1
    git push origin --delete feature-branch
    git push -u origin new-feature-1
  4. ブランチがリネームされたことを確認します。
    git branch -a

これが git branch -a を実行した結果です。

* master
new-feature-1
remotes/origin/HEAD - > origin/master
remotes/origin/master
remotes/origin/new-feature-1

まとめ

Git でリモートブランチをリネームするには、ローカルおよびリモートでブランチをリネームする必要があります。ローカルブランチをリネームするには git branch -m <古い名前> <新しい名前> コマンドを、古いリモートブランチを削除して新しいリモートブランチを設定するにはそれぞれ git push origin --delete <古い名前> および git push origin -u <新しい名前> コマンドを使用できます。