Astuce découverte : allow-unrelated-histories–Comment mettre à jour votre code local avec git ?

Vous venez de faire un git init sur votre projet local.

Vous avez git commit –m en local.


Vous souhaitez souhaitez le lier Ă  votre projet github et push tout ce que vous avez fait.
Or, vous voilà bloqué, votre repository présent sur github a déjà des fichiers.

Comment faire ?

Première étape : git remote

Vous avez d’abord lié à un repository à distance.

Cela se fait avec la commande :

git add remote origin <url>

ici, pour mon exemple :
git remote add origin https://github.com/evan-boissonnot/formation_dotnet_decouvrir_sql

Seconde étape : le commit

Vous avez commit, avec la commande : git commit –m.

Troisième étape : git push

Vous tentez alors avec insouciance un :

git push -u origin master


Impossibilité de push

! [rejected]        master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/evan-boissonnot/formation_dotnet_decouvrir_sql’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

A priori, on a du code sur le repository en remote.

Ce qui est le cas !


Quatrième étape : git pull

On tente alors un git pull.

Et là, on rencontre un nouveau problème :

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch –set-upstream-to=origin/<branch> master

Il faut spécifier la branche à tirer (pull).

Ok, on fait alors un : git pull origin master.

On tombe alors sur une erreur étrange :

fatal: refusing to merge unrelated histories

Utilisation du –allow-unrelated-histories

En lisant la documentation, nous tombons sur le conseil d’utiliser : –allow-unrelated-histories.

Cela permet d’indiquer qu’il y a des infos non historisées que l’on veut tout de même intégrer dans l’arbre des commits.

git pull origin master –allow-unrelated-histories
From https://github.com/evan-boissonnot/formation_dotnet_decouvrir_sql
  * branch            master     -> FETCH_HEAD
Merge made by the ‘recursive’ strategy.
  README.md | 2 ++
  1 file changed, 2 insertions(+)
  create mode 100644 README.md

Et voilĂ , tout fonctionne bien !

Dernière étape : on peut faire un push

On exécute un git push –u origin master, et le tour est joué !

Notre adresse

1 rue du guesclin
44000 Nantes

Notre téléphone

+33 2 79 65 52 87

Société

DevToBeCurious SARL
84860163900018 - Nantes B 848 601 639