Version control
Overview
- What are the benefits of version control?
- What is Git, and how can it be set up?
Git
Version control tracks the changes made of text-based files over time. When used properly, it enables a controlled flow of new features, and a rollback to a previous version will always be possible. Next to these major benefits, it offers a complete history of the project, including its authors. At some point, multiple people could be working in the same file, with many more on the same project. Version control ensures the changes are applied in a controlled fashion, instead of becoming an incomprehensible mess.
Git is the most commonly used CLI tool to manage version control. It is applied to a special storage system called a repository. Normally, the repository is stored in a remote location, like GitHub or GitLab, and collaborators copy this repository locally to contribute to the project. This process is called "cloning". With the desired changes made, the contributors then "push" their changes to the remote repository. This way of working is common practice among software development teams, but even when working individually, using this process can speed up development and safeguard against mistakes.
Configure Git
Git only requires a few configurations before it can be used, which can be achieved with the following command:
git config --global user.name "<NAME>"
git config --global user.email "<EMAIL>"
The configuration can be found in ~/.gitconfig. The ~ means the path to the home folder of the current user.
This is usually C:\users\<USER> on Windows systems and /home/<USER> on Unix-based systems.
When using Git on Unix-based systems, the following configuration can be useful as well:
git config --global core.editor vim
Vim is a terminal user interface (TUI), which sits between a CLI and a GUI. It offers an interface to read and edit text, but the commands still have to be typed. The following commands are used in this workshop:
:, start a command.esc, clear the command or exit editing mode.i, enter editing mode.:wq, save the changes and exit.:q!, discard the changes and exit.
Like the Bash hotkeys, there are many more commands, which can be found online.
Clone a remote repository
Most projects start with an initialised remote repository. Initialised means a default branch is available, usually with a file like a README. The concept of branches is covered in the next section of this workshop. To clone a remote repository:
git clone "<URL>"
Note
Most providers offer an HTTPS and an SSH option to clone repositories with. In this workshop the SSH option is used, as it is safer.
After cloning with HTTPS, it could be useful to store the credentials when prompted for them. This way, they don't need to be re-typed every time. To configure the helper for the credentials:
git config credential.helper store
Note that when the --global flag is not used, the configuration is only valid for the current repository.
The credentials can be found (and deleted) in ~/.git-credentials. When using SSH, the helper doesn't need to be used.
Create a local repository
It is possible to start a local repository with Git, use its version control features, and optionally push it to a remote repository later. To initialise a local repository in the current directory:
mkdir "<REPOSITORY>"
cd "<REPOSITORY>"
git init
Further reading
Next up is the basic work cycle with Git. In this section, frequently used commands are discussed to create changes in the repository. The subsequent section delves further into Git with more advanced scenarios, like merge conflicts and code reviews.