Ceci est une ancienne révision du document !
Earlier this month, I received an email from a reader of Command & Conquer, asking me to write an article on using Git – specifically things such as what a fork is, what pulling is, and what exactly a commit is. He also followed it up asking about automerge errors and how to fix them. I will do my best to cover each of these points in particular. However, as most of my experiences with Git are via Github, which offers some extra functionality on their website that isn't the “vanilla” Git experience, there may be some aspects of my explanations that do not apply to a custom git server. General Information In Git, you can create a repository (which typically contains a master branch). However, a repository can contain multiple branches – such as stable, testing, and development. A repository can be forked by anyone, and that will result in a local repository for the user who forked it. A Branch A branch could essentially be considered a snapshot of your project at a certain point. Some people advocate assigning every change to a branch, while some people keep only one or two active branches (while keeping the master branch as a basis for any future branches). Via example: If you are actively developing a web browser, you could keep two branches – stable and testing. • Stable contains the source code for your last official release – which is hopefully bug-free. • In order to keep development moving along, you would also have a branch called Testing, which contains all the bleeding edge code. You can track your changes, and get input from beta testers, in order to work your way towards a new update to stable. Once you reach a point in testing that's stable and running as you'd like it, you would then update those changes to stable, and keep working on testing.
Au début du mois, j'ai reçu le courriel d'un lecteur de Command & Conquer, me demandant d'écrire un article sur l'utilisation de Git; en particulier sur des choses comme ce qu'est un « fork » (branche), ce que veut dire « pulling » (télécharger), et ce qu'est exactement un « commit » (validation). Il a également poursuivi sur les erreurs d'« automerge » et comment les corriger. Je ferai de mon mieux pour parler de chacun de ces points en particulier. Cependant, comme la plupart de mes expériences avec Git est faite via Github, qui offre quelques fonctionnalités supplémentaires sur son site web et qui n'est pas l'expérience « commune » de Git, il peut y avoir certains aspects de mes explications qui ne s'appliquent pas à un serveur Git personnalisé.
Informations générales
Dans Git, vous pouvez créer un dépôt (qui contient généralement une branche maitre). Mais un dépôt peut contenir plusieurs branches (comme stable, testing et development). Un dépôt peut être créé par n'importe qui, et ceci se traduira par un dépôt local pour l'utilisateur qui le crée.
Une branche
Une branche pourrait essentiellement être considérée comme une photo de votre projet à un certain moment. Certaines personnes préconisent d'attribuer une branche à tout changement, alors que certaines personnes gardent seulement une ou deux branches actives (tout en gardant la branche master de base pour toutes les futures branches). Par exemple: Si vous développez activement un navigateur Web, vous pouvez garder deux branches : stable et testing. • Stable contient le code source de votre dernière sortie officielle, qui est sans bug, espérons-le. • Pour continuer à avancer les développement pendant ce temps, vous pouvez créer aussi une branche appelée Testing, qui contient tout le nouveau code. Vous pouvez suivre les modifications, et obtenir la contribution de bêta-testeurs, afin de cheminer dans votre travail vers une nouvelle mise à jour stable. Une fois que vous atteignez un point dans le test qui est stable et fonctionne comme vous le souhaitez, vous allez ensuite mettre à jour ces changements dans la branche stable, et continuer à travailler sur les tests.
A Fork Is when a user sees a project they like (i.e. the web browser project from my last example), and think to themselves “I could do this better”, or “I'd love to help create this”. Instead of assigning users the ability to edit the official repository and its branches, the user would instead create a fork of the project. You can imagine it as a highway (the official repository), with turn-offs for every user who is contributing to it, which leads to their local copies. This is useful, as it prevents the main goal of the original project from shifting – if you want to re-purpose the code for a web browser to create an image gallery, you can fork it as normal, and make any changes you'd like. Some of you may be wondering how these forks can help contribute anything to a project, if it's essentially a copy. This is handled by something called a merge (explained below). A Commit Before we discuss what a merge is, we should first explain commits. Whenever you change a file in your local repository, you can choose to save it as a commit (i.e. a change), give it a brief explanation, and then push (upload) it back to your remote repository, so that the updates can be propagated through all forks based off that repository, as well as give the most recent versions to any new forks.
A Merge Once you've committed changes to your remote repository, and believe that it would help the original project, you can then send a merge request for that commit (or a series of commits). This then sends a notification to the owner of the original repository, and includes information on the commit, displays a comparison of the before/after, and indicates any conflicts (if, for example, you've already changed the line of code slightly, and the patch can therefore no longer “find” it). Say you write a patch for the web browser that allows users to define their own CSS (cascading style sheets), in order to style their browser and website views to their specifications. To do so, you adjusted a for-loop that runs over all files within folders called “config” and “data”, to include the folder “styles”. However, upstream (the original project), has changed the name of the config folder to “conf”. This means that the loop looks different than expected to the merge request – which raises a conflict with the original developer. They can then choose to resolve it, or ignore it.
Resolving Conflicts If you run into conflicts with merges (i.e. two people edit the same file, or one person edits it and another deletes it), you will generally need to resolve it manually. To go about doing this, you would run git status This will give you information about what files are in conflict, as well as give you instructions to indicate when you have solved the conflict. Viewing the actual conflict is as simple as opening the file after the merge has failed – the file should contain a block that looks something like: the number of planets are «««< HEAD nine ======= eight »»»> branch-a
The series of less than/greater than signs mark the area of the conflict, and the line of equals signs mark the two different changes. Branch-a refers to which branch this conflict is occurring in. To resolve this, simply delete the conflict markers (less than, greater than, and the equals signs), as well as deleting either your change, the other person's change, or replacing the entirety of the conflict with a new edit (i.e. something that contains both edits). Once the markers are gone, you can go about adding it to the commit list, and pushing it to the remote repository. In the case of a conflict caused by someone deleting a file, you can resolve it in one of two ways: • Adding the file back, and then committing it (which essentially overrules the commit where it was deleted). • Deleting the file with git rm, and then committing the change again. What is a Pull? A pull is a combination of git-fetch and git-merge. Fetch essentially asks for any changes to the repository, and downloads the commits. Merge then tries to integrate the new information into your existing copy of the repository. Instead of having to do these things one by one, Git instead offers the pull command, which automatically attempts to merge anything downloaded via fetch.
What, no code?! This month, I decided I would focus on explaining the terminology and illustrate some aspects of Git. Next month, I will run you through a series of examples for setting up a git repository, cloning it, editing the branches, creating a commit and resolving a conflict. For anyone who does not want to wait a month, there are links to information in the further reading section below. Hopefully this article has helped to shine some light on the terminology of Git. If you have questions, comments, recommendations, or suggestions, you are welcome to email me at lswest34+fcm@gmail.com. Further Reading http://git-scm.com/doc – Git Documentation http://githowto.com/ - Git Howto website https://help.github.com/ - Github offers some well-written explanations for the typical uses of Git, as well as explaining Github-specific features well.