Version control and Git
What is Version Control and Git?
A Version Control System (VCS) is a tool or a methodology that enables developers to track changes to their files, typically code files, over time and to reinstate previous versions of these files if something was to go wrong.
A very primitive example of a VCS, one that we probably all do or have done at one time, is the series of changes we make to a word document and the different saved version of that same file as shown below:
In VCS, this would just be a single file with the multiple versions stored somewhere in the background and can be recalled many times.
Before Git, previous VCSs were invented, such as CVS and SVN. These VCSs were based on a central repository model, which means that there was ever only a single copy of the "master" source code hosted in a remote environment.
Unlike CVS and SVN, Git is a distributed VCS. This means that every developer has their own copy of the "master" source code which they can edit locally on their computer (this comes with many benefits as outlined later on). It is not necessary to have a central, remote repository, but by convention one is established. Usually this remote repository is hosted on a platform like Github.
Three Tree Architecture of Git
In Git, edits to code are called change sets or patches. A single change set is a discrete unit which Git tracks.
To better understand Git, imagine there are three layers or trees as shown above:
- Working Directory - This is the layer in which a developer makes changes to a file or to multiple files in a single project (git repo). This is essentially the set of folders/files that you actually see and edit on your local computer.
- Staging Index - This is an intermediate step between the working directory and the local repository. The purpose of this layer is to provide a preview of the change sets and separate out changes of the same type as a single change set before actually committing them forever.
- Local Repository - This is essentially a 1:1 mirror copy of the working directory and it is where all commits are saved.
Benefits of Git:
- Developers can work independently
- No need to communicate with a central server
- No single point of failure
- Fosters collaboration between developers