TechDocs/Git/Workflow

Common information

The workflow in Git is quite different from what you may be used to. This is because Git is decentralised. Every repository in Git is equal, but some repositories are more equal than others.

Before you start, make sure you have everything set up as described in TechDocs/Git/FirstSteps. Also make sure you understand basic Git commands as described in TechDocs/Git/FirstCommit.

This page explains two different scenarios.

  1. A simple approach with direct edits to the Git repository. It's easier to understand but bears a few risks.

  2. A more complex approach which involves forks and Pull Requests. This is safer and allows more checks. For example, we highly prefer this for changes on the fsfe.org website.

It will start with the simple approach which skips a lot of detailed information about the different aspects Git it built upon. If you don't understand something, please have a look into the complex approach, or skim external resources like the excellent Visual guide to version control and its following guides in the series.

Simple approach

This workflow reduces complexity by giving up the need of forks, extra branches and pull requests. While it's easier to learn, we require the complex approach for some repositories like the website because it's safer, more transparent and more reliable. Also, this method requires that you have at least write access to the repository.

In this example, it is assumed you would like to work on FSFE/share-buttons, an interesting but seldom used repository.

Basic knowledge

As with the complex workflow, you should rudimentally know about the scheme you are working with.

The Two Repositories

There are only two repositories you are working with:

Branches

Unlike with the complex approach, branching will play no role in most repositories in which the simple workflow can be used. You will only work with the master branch which is the official "timeline" of the repository. Any change that you will make and upload to this branch will directly affect the local repositories of the others working on it as soon as they update to the latest status.

One-time preparations

In the following you'll find the steps which have to be execute only once per repository you want to work on. It is assumed that you successfully set up Git on your computer according to our recommendations.

  1. Clone the repository to a place on your computer. Within a terminal, move to a location of your choice and execute git clone git@git.fsfe.org:FSFE/share-buttons.git. This will create a folder called share-buttons inside the directory your terminal has opened.

Workflow

After the one-time preparations, the following steps have to be execute in a terminal positioned in your local repository each time you would make an edit.

  1. Before making any change, always do a git pull. This will update your local copy with the remote repository to get the latest changes.

  2. Now you can make any changes, e.g. create new files or edit existing ones. You can do that in your file browser.
  3. Run git status to be presented with a list of edits you have done. This can be executed any time and will help you to understand the difference between the official status and your own edits.

  4. In order to share your changes, you will have to add them. This has to be done for new, modified and deleted files, and will add them to the list of files you would like to share your modifications for: git add new_file1.txt modified_file2.jpg. Note that this can also be directories, and that you can add multiple files at once!

  5. Now let's make it official! By committing your edits, you are required to describe them in order to allow others to understand them: git commit -m 'I did some modifications in order to solve this and that problem'. Please be informative and concise at the same time.

  6. Your local copy has been changed, now push the changes online to all other people working on the repository: git push. That makes it official, and you can see it online.

Due to the disadvantages of this workflow, you shouldn't let pass too much time between the first and last step in order to avoid conflicts with potential edits of other people. Also, please make sure to double-check your edits since there is no check in between.

Complex approach

The complex workflow is recommended for intensively used repositories which are affected by many changes by multiple users. You will not work on the official repository directly but on your own copy ("fork") and ask a maintainer to include your changes to the official repositories in an extra step. Also, you can contribute to any public repository even if you don't have write access.

This guide is also written in a more verbose form than the simple approach. even if you feel confident that you are not required to know the advanced workflow, it probably will still help you to understand Git a bit better.

Basic knowledge

The three repositories

As a beginner, you only really need to know about three repositories that are relevant to you, and that you will interact with. For this guide, we will assume that we are working on fsfe-website. The three repositories are:

These three terms will consistently be used in this guide. However, in the simple guide, the forked origin is not used as you are directly working on the main repository.

Branches

In Git, you will often be using branches. When you first visit a project's web page or first clone a project, you will see the master branch of that repository. This is the main branch in any repository.

Consider a branch like an independent alternate timeline of commits, where the master branch is the main timeline. So if you create a new branch from master, you are entering an alternate timeline that, from that point onward, is not affected by changes in master. You can merge that timeline back into master, though this is often done by the maintainer of the repository.

Similarly, when a new commit is added to the master branch of the main remote repository, that commit does not immediately appear on the master branch of your remote repository. You have to manually update this. This makes sense, because your repository is independently your own, and nobody else's.

You usually do not edit the master branch directly. Ideally, the master branch is identical between the main remote repository, your remote repository and your local repository, so that all repositories agree on the official series of commits. If you edit your own master branch, you are basically disagreeing with the main remote repository and creating your own "official" timeline. Which would be fine, if you were writing an alternate history novel, but you probably want to stick to the official master branch.

Workflow

This guide is way to create a new pull request, i.e. a proposal for an addition or change of yours to the upstream repository, mostly via the terminal. This solution is preferred over using graphical applications, because it is easier to ask someone to type a certain command, than it is to ask them to push some buttons.

Set up your repository

/!\ Please, keep in mind that for setting up git and cloning repository you will need from 2 to 3 hours of time! So it's better to do this when you come to the office in the morning.

Assuming you have set up your remote repository by clicking the "Fork" button on the main project page, you can now set up your local repository.

Also, by cloning, you have implicitly configured a "remote" for your local repository. This remote is called origin, and is equal to "git@git.fsfe.org:YOURNAME/fsfe-website.git". You might want to add an extra remote, however, pointing to the main remote repository. Let's do this now, and call it upstream.

Syncing all repositories

Before you start, you may want to synchronise all repositories so that you are working on the latest version of the main remote repository (upstream).

Start on a new feature

Let's begin changing something, e.g. adding a new translation. Assuming that your local repository is up-to-date, you can create a new branch from master. We never make any changes directly to the master branch.

At this point, you leave Git alone and begin working on your changes. You only return to Git after you are done.

When you are done, you add files to the staging area. This is basically a list of files that you would like to commit all at once. Everything outside of the staging area is not committed. When the staging area is properly populated, you can commit. By committing, you are basically declaring that you are confident in your changes, that they form a single cohesive whole, and that they are more-or-less complete.

Sharing your changes

If you did the above, you have now added a commit to a branch in your local repository. But you want to eventually get that commit into the master branch of the main remote repository. To do that, you have to follow a few steps.

First, you upload the branch with commits to your remote repository (origin).

When you go to https://git.fsfe.org/YOURNAME/fsfe-website, your commit should now be visible in its own branch! This is good, because it means that your change is available online, and that others can view it. This also means that the maintainer of the main remote repository can view your changes.

To ask the maintainer to pull your branch into the master branch of the main remote repository, you create a pull request. You do this by navigating to the main webpage the main remote repository and clicking on "Pull Requests". There, you can create a new pull request and specify that you would like the maintainer to update master (main) with NAME-OF-FEATURE (compare). Add a title and a description, and your work is done :-). The maintainer will review your changes and accept them when everything is okay.

Helpful commands

Below an uncomplete list of commands that prove to be useful.

Questions

This guide is, and will probably always be, incomplete. Git is a very advanced piece of software. If you run into a problem that this guide does not explain, please ask a technically inclined person to add a section to the guide that sufficiently explains how to deal with that problem.

There are also very many guides on how to use Git on the internet. A visual guide to version control and its whole series explain it from a more non-technical side. Atlassian and GitHub have some very detailed tutorials that may help you with more specific things.

TechDocs/Git/Workflow (last edited 2018-10-30 16:31:01 by max.mehl)