git: ブランチの名前を変更するにはどうすればいいですか (ローカルとリモートの両方)? 質問する

git: ブランチの名前を変更するにはどうすればいいですか (ローカルとリモートの両方)? 質問する

masterリモート ブランチを指すローカル ブランチがありますorigin/regacy(おっと、タイプミスです!)。

リモート ブランチの名前を または に変更するにはどうすればよいですorigin/legacyorigin/master?

私は試した:

git remote rename regacy legacy

しかし、次のようなエラーが発生しました:

エラー: 構成セクション 'remote.regacy' の名前を 'remote.legacy' に変更できませんでした

ベストアンサー1

図式的でかわいい Git リモート グラフ


これを実現するにはいくつかの方法があります。

  1. ローカルブランチを変更し、変更をプッシュします
  2. ローカルでは元の名前を保持したまま、新しい名前でブランチをリモートにプッシュします。

ローカルとリモートの名前変更

# Names of things - allows you to copy/paste commands
old_name=feature/old
new_name=feature/new
remote=origin

# Rename the local branch to the new name
git branch -m $old_name $new_name

# Delete the old branch on remote
git push $remote --delete $old_name

# Or shorter way to delete remote branch [:]
git push $remote :$old_name

# Prevent git from using the old name when pushing in the next step.
# Otherwise, git will use the old upstream name instead of $new_name.
git branch --unset-upstream $new_name

# Push the new branch to remote
git push $remote $new_name

# Reset the upstream branch for the new_name local branch
git push $remote -u $new_name

コンソールのスクリーンショット


リモートブランチのみの名前変更

クレジット:プティム

# In this option, we will push the branch to the remote with the new name
# While keeping the local name as is
git push $remote $remote/$old_name:refs/heads/$new_name :$old_name

重要な注意点:

(move)を使用するとgit branch -m、Git は追跡ブランチも新しい名前で更新します。

git remote rename legacy legacy

git remote rename設定ファイル内のリモート セクションを更新しようとしています。指定された名前のリモートを新しい名前に変更しますが、この場合は何も見つからなかったため、名前の変更に失敗しました。

しかし、それはあなたが考えているようには動作しません。リモート ブランチ ではなく、ローカル構成のリモート名が変更されます。


注: Git サーバーでは、Web インターフェイスまたは外部プログラム (Sourcetree など) を使用して Git ブランチの名前を変更できる場合がありますが、Git ではすべての作業がローカルで実行されるため、上記のコマンドを使用して作業することをお勧めします。

おすすめ記事