Comment puis-je pousser une nouvelle branche locale vers un dépôt Git distant et la suivre aussi?
Je veux être capable de faire ce qui suit:
Créer une branche locale basée sur quelques autres (distant ou local) direction générale (via
git branchougit checkout -b)Poussez la branche locale au référentiel distant (publier), mais le rendre trackable donc
git pulletgit pushfonctionneront immédiatement.
Comment puis-je faire ça?
Je connais --set-upstream dans Git 1.7, mais c'est une action post-création. Je veux trouver un moyen de faire un changement similaire en poussant la branche vers le référentiel distant.
13 réponses
Dans Git 1.7.0 et versions ultérieures, vous pouvez extraire une nouvelle branche:
git checkout -b <branch>
Modifier les fichiers, ajouter et valider. Puis poussez avec le -u (abréviation de --set-upstream) option:
git push -u origin <branch>
Git va configurer les informations de suivi pendant le push.
Si vous ne partagez pas votre repo avec d'autres, Ceci est utile pour pousser toutes les vos branches vers la télécommande, et --set-upstream suivre correctement pour vous:
git push --all -u
(pas exactement ce que l'OP demandait, mais ce one-liner est assez populaire)
Si vous partagez votre repo avec d'autres, ce n'est pas vraiment une bonne forme car vous allez obstruer le repo avec toutes vos branches expérimentales douteuses.
Avant l'introduction de git push -u, il n'y avait pas d'option git push pour obtenir ce que vous désirez. Vous avez dû ajouter de nouvelles instructions de configuration.
Si vous créez une nouvelle branche en utilisant:
$ git checkout -b branchB
$ git push origin branchB:branchB
Vous pouvez utiliser la commande git config pour éviter de modifier directement le fichier .git/config.
$ git config branch.branchB.remote origin
$ git config branch.branchB.merge refs/heads/branchB
Ou vous pouvez modifier manuellement le fichier .git/config pour avoir des informations de suivi sur cette branche.
[branch "branchB"]
remote = origin
merge = refs/heads/branchB
En termes simples, pour créer une nouvelle branche locale , Faites:
git branch <branch-name>
Pour le pousser vers le référentiel distant , Faites:
git push -u origin <branch-name>
Une légère variation des solutions déjà données ici:
-
Créez une branche locale basée sur une autre branche (distante ou locale):
git checkout -b branchname -
Poussez la branche locale vers le référentiel distant (publish), mais rendez-la traçable pour que
git pulletgit pushfonctionnent immédiatementgit push -u origin HEADL'utilisation de
HEADest un "moyen pratique de pousser la branche actuelle au même nom sur la télécommande". Source: https://git-scm.com/docs/git-push En termes de Git, HEAD (in majuscule) est une référence au sommet de la branche courante (arbre).L'option
-uest juste courte pour--set-setupstream. Cela ajoutera une référence de suivi en amont pour la branche actuelle. vous pouvez vérifier cela en regardant dans votre .fichier git / config:
Je suppose que vous avez déjà cloné un projet comme:
git clone http://github.com/myproject.git
-
Ensuite, dans votre copie locale, créez une nouvelle branche et vérifiez-la:
git checkout -b <newbranch> -
Supposons que vous ayez créé un "git bare --init" sur votre serveur et créé le myapp.git, vous devriez:
git remote add origin ssh://example.com/var/git/myapp.git git push origin master -
Après cela, les utilisateurs devraient pouvoir
git clone http://example.com/var/git/myapp.git
NOTE: je suppose que votre serveur est opérationnel. Si elle ne l'est pas, ça ne marchera pas. Une bonne façon à est ici.
Ajouté
Ajouter une branche distante:
git push origin master:new_feature_name
Vérifiez si tout est bon (récupérez l'origine et listez les branches distantes):
git fetch origin
git branch -r
Créez une branche locale et suivez la branche distante:
git checkout -tb new_feature_name origin/new_feature_name
Tout mettre à jour:
git pull
Je fais simplement
git push -u origin localBranch:remoteBranchToBeCreated
Sur un projet déjà cloné.
Git crée une nouvelle branche nommée remoteBranchToBeCreated sous mes commits que j'ai faits dans localBranch.
Edit Obsolète, il suffit d'utiliser git push -u origin $BRANCHNAME
Utilisez git publish-branch à partir des outils Git divers de William (gitorious repo et clone ).
OK, pas de Ruby, donc-en ignorant les sauvegardes! - prenez les trois dernières lignes du script et créez un script bash, git-publish-branch:
#!/bin/bash
REMOTE=$1 # Rewrite this to make it optional...
BRANCH=$2
# Uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}
Ensuite, exécutez git-publish-branch REMOTENAME BRANCHNAME, où REMOTENAME est généralement origin (vous pouvez modifier le script pour prendre origin par défaut, etc...)
Pour créer une nouvelle branche en se détachant de la branche existante
git checkout -b <new_branch>
Puis poussez cette nouvelle branche vers le référentiel en utilisant
git push -u origin <new_branch>
Cela crée et pousse tous les commits locaux vers une branche distante nouvellement créée origin/<new_branch>
J'ai fait un alias de sorte que chaque fois que je crée une nouvelle branche, il va pousser et suivre la branche distante en conséquence. J'ai mis le morceau suivant dans le fichier .bash_profile:
# Create a new branch, push to origin and track that remote branch
publishBranch() {
git checkout -b $1
git push -u origin $1
}
alias gcb=publishBranch
Utilisation : tapez simplement gcb thuy/do-sth-kool avec thuy/do-sth-kool est mon nouveau nom de branche.
Pour la version GitLab antérieure à 1.7, utilisez:
git checkout -b name_branch
(name_branch, ex: master)
Pour le pousser vers le référentiel distant, faites:
git push -u origin name_new_branch
(name_new_branch, exemple: fonction)
En M'appuyant légèrement sur les réponses ici, j'ai enveloppé ce processus comme un simple script Bash, qui pourrait bien sûr être également utilisé comme alias Git.
L'ajout important pour moi est que cela m'invite à exécuter des tests unitaires avant de valider et passe dans le nom de la branche actuelle par défaut.
$ git_push_new_branch.sh
Have you run your unit tests yet? If so, pass OK or a branch name, and try again
usage: git_push_new_branch {OK|BRANCH_NAME}
e.g.
git_push_new_branch -> Displays prompt reminding you to run unit tests
git_push_new_branch OK -> Pushes the current branch as a new branch to the origin
git_push_new_branch MYBRANCH -> Pushes branch MYBRANCH as a new branch to the origin
Git_push_new_branch.sh
function show_help()
{
IT=$(CAT <<EOF
Have you run your unit tests yet? If so, pass OK or a branch name, and try again
usage: git_push_new_branch {OK|BRANCH_NAME}
e.g.
git_push_new_branch.sh -> Displays prompt reminding you to run unit tests
git_push_new_branch.sh OK -> Pushes the current branch as a new branch to the origin
git_push_new_branch.sh MYBRANCH -> Pushes branch MYBRANCH as a new branch to the origin
)
echo "$IT"
exit
}
if [ -z "$1" ]
then
show_help
fi
CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$1" == "OK" ]
then
BRANCH=$CURR_BRANCH
else
BRANCH=${1:-$CURR_BRANCH}
fi
git push -u origin $BRANCH
Pour télécharger votre branche locale d'un référentiel public, vous devez cd vers le référentiel public, puis utiliser le code suivant:
git push -u origin branchname
