Renommer une branche distante

GitGitBeginner
Pratiquer maintenant

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

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

En Git, une branche est un pointeur léger et mobile vers un commit. Renommer une branche est une tâche courante en Git. Cependant, renommer une branche localement et sur le distant peut être un peu difficile. Dans ce laboratoire, vous allez apprendre à renommer une branche distante en 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{{"Renommer une branche distante"}} end

Renommer une branche distante

Pour terminer ce laboratoire, vous utiliserez le référentiel Git git-playground de votre compte GitHub, qui provient d'un fork de https://github.com/labex-labs/git-playground.git. Veuillez décocher "Copier seulement la branche master" lors du forking.

Vous disposez d'un référentiel Git nommé https://github.com/your-username/git-playground. Vous avez créé une branche nommée feature-branch et l'avez poussée sur le distant. Maintenant, vous voulez renommer la branche en new-feature-1 à la fois localement et sur le distant.

  1. Clonez le référentiel, accédez au répertoire et configurez l'identité :
    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. Basculez sur la branche nommée feature-branch :
    git checkout feature-branch
  3. Renommez la branche à la fois localement et sur le distant :
    git branch -m feature-branch new-feature-1
    git push origin --delete feature-branch
    git push -u origin new-feature-1
  4. Vérifiez que la branche a été renommée :
    git branch -a

Voici le résultat de l'exécution de git branch -a :

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

Résumé

Renommer une branche distante en Git consiste à renommer la branche à la fois localement et sur le distant. Vous pouvez utiliser la commande git branch -m <ancien-nom> <nouveau-nom> pour renommer la branche locale et les commandes git push origin --delete <ancien-nom> et git push origin -u <nouveau-nom> pour supprimer l'ancienne branche distante et définir la nouvelle branche distante, respectivement.