Introduction au contrôle de version Git
Important
A more in-depth guide on Git is available on the Git website.
Git is a Distributed Version Control System (VCS) created by Linus Torvalds, also known for creating and maintaining the Linux kernel. Version Control is a system for tracking changes of code for developers. The advantages of Git Version Control are:
Separation of testing environments into branches
Il offre la possibilité de naviguer vers un commit particulier sans supprimer l’historique
Il offre la capacité à gérer les commits de différentes manières, y compris en les combinant
Et comporte diverses autres fonctionnalités, voir ici
Conditions préalables
Important
Ce didacticiel utilise le système d’exploitation Windows
Vous devez télécharger et installer Git à partir des liens suivants:
Note
Vous devrez peut-être ajouter Git à votre chemin de répertoire
Le vocabulaire Git
Git revolves around several core data structures and commands:
Repository (Référentiel de données): la structure des données de votre code, y compris un dossier
.git
dans le répertoire racineCommit: a particular saved state of the repository, which includes all files and additions
Branch: a means of grouping a set of commits. Each branch has a unique history. This is primarily used for separating development and stable branches.
Push (Pousser): mettez à jour le référentiel distant avec vos modifications locales
Pull (Tirer): mettez à jour votre référentiel local avec les modifications à distance
Clone: retrieve a local copy of a repository to modify
Fork: duplicate a pre-existing repository to modify, and to compare against the original
Merge: combine various changes from different branches/commits/forks into a single history
Le Référentiel de données
Un référentiel Git est une structure de données contenant la structure, l’historique et les fichiers d’un projet.
Les référentiels Git comprennent généralement:
Un dossier
.git
. Ce dossier contient les différentes informations sur le référentiel.Un fichier
.gitignore
. Ce fichier contient les fichiers ou répertoires que vous ne voulez pas inclure lors de la validation.Fichiers et dossiers. Il s’agit du contenu principal du référentiel.
Création du Référentiel de données
You can store the repository locally, or through a remote – a remote being the cloud, or possibly another storage medium or server that hosts your repository. GitHub is a popular free hosting service. Numerous developers use it, and that’s what this tutorial will use.
Note
There are various providers that can host repositories. Gitlab and Bitbucket are a few alternatives to Github.
Création d’un compte GitHub
Go ahead and create a GitHub account by visiting the website and following the on-screen prompts.

Création locale
Après avoir créé et vérifié votre compte, vous irez ensuite visiter la page d’accueil. Cela ressemblera à l’image montrée ci-dessous.

Cliquez sur l’icône « plus », ou + en haut à droite.

Cliquez ensuite sur « New Repository »

Remplissez les informations appropriées, puis cliquez sur « Create repository »

Vous devriez voir un écran similaire à celui-ci

Note
The keyboard shortcut Ctrl+~ can be used to open a terminal in Visual Studio Code for Windows.
Now you’ll want to open a PowerShell window and navigate to your project directory. An excellent tutorial on PowerShell can be found here. Please consult your search engine on how to open a terminal on alternative operating systems.

If a directory is empty, a file needs to be created in order for git to have something to track. In the below Empty Directory example, we created a file called README.md
with the contents of # Example Repo
. For FRC® Robot projects, the below Existing Project commands should be run in the root of a project created by the VS Code WPILib Project Creator. More details on the various commands can be found in the subsequent sections.
Note
Remplacez le chemin d’accès "C:\Users\ExampleUser9007\Documents\Example Folder"
par celui dans lequel vous souhaitez créer le repo, et remplacez l’URL distante https://github.com/ExampleUser9007/ExampleRepo.git
par l’URL du repo que vous avez créé dans les étapes précédentes.
> cd "C:\Users\ExampleUser9007\Documents\Example Folder"
> git init
Initialized empty Git repository in C:/Users/ExampleUser9007/Documents/Example Folder/.git/
> echo "# ExampleRepo" >> README.md
> git add README.md
> git commit -m "First commit"
[main (root-commit) fafafa] First commit
1 file changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README.md
> git remote add origin https://github.com/ExampleUser9007/ExampleRepo.git
> git push -u origin main
> cd "C:\Users\ExampleUser9007\Documents\Example Folder"
> git init
Initialized empty Git repository in C:/Users/ExampleUser9007/Documents/Example Folder/.git/
> git add .
> git commit -m "First commit"
[main (root-commit) fafafa] First commit
1 file changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README.md
> git remote add origin https://github.com/ExampleUser9007/ExampleRepo.git
> git push -u origin main
Les Commits, ou Validés
Les référentiels sont principalement composés de Commits. Ceux-ci sont des états enregistrés ou des versions de code.
Dans l’exemple précédent, nous avons créé un fichier appelé README.md. Ouvrez ce fichier dans votre éditeur de texte préféré et modifiez-en quelques lignes. Après avoir joué avec le fichier pendant un moment, il suffit d’enregistrer et de fermer. Accédez à PowerShell et tapez les commandes suivantes.
> git add README.md
> git commit -m "Adds a description to the repository"
[main bcbcbc] Adds a description to the repository
1 file changed, 2 insertions(+), 0 deletions(-)
> git push
Note
Writing good commit messages is a key part of a maintainable project. A guide on writing commit messages can be found here.
La commande Git Pull
Note
git fetch
peut être utilisé lorsque l’utilisateur ne souhaite pas fusionner automatiquement dans la branche de travail actuelle
Cette commande récupère l’historique ou valide à partir du référentiel distant. Lorsque la télécommande contient du travail que vous n’avez pas, elle tentera de fusionner automatiquement. Voir Le fusionnement (Merge).
Run: git pull
La commande Git Add
This command « stages » the specified file(s) so that they will be included in the next commit.
For a single file, run git add FILENAME.txt
where FILENAME.txt is the name and extension of the file to add.
To add every file/folder that isn’t excluded via gitignore,
run git add .
. When run in the root of the repository this command will stage every untracked, unexcluded file.
La commande Git Commit
This command creates the commit and stores it locally. This saves the state and adds it to the repository’s history. The commit will consist of whatever changes (« diffs ») were made to the staged files since the last commit. It is required to specify a « commit message » explaining why you changed this set of files or what the change accomplishes.
Exécuter: git commit -m "type message here"
La commande Git Push
Téléchargez (Push) vos modifications locales sur le « Cloud » (à distance)
Exécuter: git push
Les Branches
Branches in Git are similar to parallel worlds. They start off the same, and then they can « branch » out into different varying paths. Consider the Git control flow to look similar to this.
Dans l’exemple ci-dessus, la branche principale a été fusionnée (ou dupliquée) avec la branche dévolue à la Fonctionnalité 1 et quelqu’un a dupliqué la branche pour en avoir une copie locale. Ensuite, une personne fait un commit (ou téléchargé) de ses modifications, les fusionnant avec la branche de Fonctionnalité 1 de la branche. Vous « fusionnez » les changements d’une branche à l’autre.
Création d’une branche
Exécutez: git branch branch-name
où branch-name est le nom de la branche à créer. Le nouvel historique de branche sera créé à partir de la branche active actuelle.
Entrer dans une branche
Une fois qu’une branche est créée, vous devez ensuite entrer dans la branche.
Exécutez: git checkout branch-name
où branch-name est la branche qui a été créée précédemment.
Le fusionnement (Merge)
In scenarios where you want to copy one branches history into another, you can merge them. A merge is done by calling git merge branch-name
with branch-name being the name of the branch to merge from. It is automatically merged into the current active branch.
It’s common for a remote repository to contain work (history) that you do not have. Whenever you run git pull
, it will attempt to automatically merge those commits into your local copy. That merge may look like the below.
However, in the above example, what if File A was modified by both branch Feature1 and Feature2? This is called a merge conflict. A merge conflict can be resolved by editing the conflicting file. In the example, we would need to edit File A to keep the history or changes that we want. After that has been done, simply re-add, re-commit, and then push your changes.
Les réinitialisations (Resets)
Parfois, l’historique doit être réinitialisé ou un Commit doit être annulé. Cela peut se faire de plusieurs manières.
Revenir en arrière sur un Commit
Note
Vous ne pouvez pas annuler une fusion, car git ne sait pas quelle branche ou origine il doit choisir.
Pour revenir en arrière sur l’historique menant à un Commit, exécutez git revert commit-id
. Les ID de validation peuvent être affichés à l’aide de la commande git log
.
La commande de réinitialisation de la tête
Avertissement
Réinitialiser la tête est une commande dangereuse. Elle efface définitivement toute le travail que vous avez accompli, et qui n’a pas été validé (Commit). Vous êtes prévenus!
Exécutez: git reset --hard commit-id
.
Fourches ou Forks
Les fourches peuvent être traitées de la même manière que les branches. Vous pouvez fusionner le référentiel d’origine, (celui en amont, ou upstream) dans le référentiel fourché (celui d’origine).
Clonage d’un Dépôt existant
Dans la situation où un dépôt a déjà été créé et stocké dans un serveur distant, vous pouvez le cloner à l’aide
git clone https://github.com/myrepo.git
where myrepo.git
is replaced with your git repo. If you follow this, you can skip to commits.
Mettre à jour une fourche
Ajoutez l’amont:
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Confirmez qu’il a été ajouté via:
git remote -v
Faire un « Fetch » sur les changements en amont:
git fetch upstream
Fusionnez les modifications dans la tête:
git merge upstream/upstream-branch-name
Gitignore
Important
Il est extrêmement important que les équipes ne modifient pas le fichier .gitignore
qui est intégré dans leur projet de robot. Cela peut conduire à un déploiement hors connexion qui ne fonctionne pas.
A .gitignore
file is commonly used as a list of files to not automatically commit with git add
. Any files or directory listed in this file will not be committed. They will also not show up with git status.
Additional Information can be found here.
Masquer un dossier
Ajoutez simplement une nouvelle ligne contenant le dossier à cacher, avec une barre oblique à la fin
EX: répertoire à exclure/
Masquer un fichier
Ajoutez une nouvelle ligne avec le nom du fichier à masquer, y compris tout répertoire précédant de la racine du référentiel.
EX: répertoire/fichier-à-cacher.txt
EX: file-to-hide2.txt
Information supplémentaire
A much more in-depth tutorial can be found at the official git website.
A guide for correcting common mistakes can be found at the git flight rules repository.