“How to Git” series – An introduction on what is Git and why to use it – #01 Setting Things Up
What is Git?
Git is an Open Source Version Control System, that was originally developed by Linus Torvalds in 2005. Since then, it became the worldwide solution for collaborative work.
Git creates a local version of the project (repository) on your machine and then tracks changes to the files within that project. That way you have a history of the changes you (or your co-workers) have done to the project, and you can also revert at any time of the project should you run into problems.
Git can either be used via command line (in your terminal) or with a Graphical User Interface in a desktop app. Many IDEs and Code editors also come with Plugins to manage your repositories. Basic knowledge about the command line usage can be helpful, because not every job position works with your preferred tool. Also, there is a high chance that the GUI tool you use does not support all the functions the command line would.
Git can be used local, but also collaborative with server side solutions like GitLab or GitHub to work with co-workers a-synchronically.
Since it is a distributed and not a centralized system, you don't have to rely on permanent access to the server that holds the latest code but rather work locally and then submit once your changes are made- or even just exchange it with your co-workers.
Git has become successful because it is free, fast, and scalable. Learning Git can be tricky and that's why I would suggest anyone starting to learn programming, to put Git on top of the lists of things they need to know in order to navigate through the programming world.
Installation
To see if you have Git installed and which version, you start your terminal on your computer and type:
git --version
If Git is not installed you will get a message like:
git: command not found
or
'git' is not recognized as an internal or external command, operable program, or batch file.
If it is installed, you will get a version information instead.
Go to https://git-scm.com/downloads and compare your version with the up-to-date version available. Update if needed. You can either download the external downloader or use the command line. The website above will give you the appropriate options depending on your OS.
Configure Git
Before you can start using git, you will have to do some configuration work. It will make your life easier especially when working together in a collaborative space. Settings in Git can be done in 3 layers— System: for the whole machine you're working on, Global: your repositories only or Local: for each repository individual. [System - Global - Local] can overwrite the settings of the previous one in the list, so usually setting up your credentials globally is enough. If you need to change them for a specific repository you can still overwrite your default later with a local setting.
With
git config --global user.name "John Doe"
you set up your name. Make sure to put it into quotation marks.
Next you need to put in your email address. You use this command for it:
git config --global user.email johndoe@example.com
You will not need quotation marks here since you are not using spaces.
Next you should setup your preferred Editor for git. I use VS-Code, so I use the command:
git config --global core.editor "code --wait"
For Atom you would use:
git config --global core.editor "atom --wait"
If you want to use your own program like let's say Notepad++, you can also use the direct file path to the executable like here:
git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
While there are a ton more settings that you can have a look at in the git documentation, I want to point out one more that should be set. It is how git should handle line endings. Depending on what OS you use and what OS your co-workers use, you can run into issues with line endings because different OS handle line endings differently.
When you are on Windows and you are either not sure what your co-workers use or you want to leave the option open to work with different people, set up:
git config --global core.autocrlf true
When you are on Linux or macOS, you want to use:
git config --global core.autocrlf input
If you only work with windows and all your co-workers do, you can technically set it to
git config --global core.autocrlf false
in order to disable it, but the common practice is to rather use the "true".
If you want to see all the settings you have done, open your config file with
git config --global -e
You can even make changes later on in this file directly. Setting it up first via the command line first ensures that you don't run into problems later due to falsely formatted input.
Well done! You have set up your Git successfully, and you are ready to use it.