In this tutorial we will learn basic concepts of Git.
When we create a project we also initialize a Git repository by running the git init command. More on that in the next tutorial. This command creates a .git directory in the project folder which is the local repository and it keeps all the changes made to the project.
In this setup we have a central server that keeps all the changes made to the project Git repository. Then we have the client machines who copy the repository to their respective computer to work locally on the project.
Example: GitHub.com uses Git and we can create FREE account to save our repositories which can later be cloned (copied) by clients.
The changes we make in a project file is saved as snapshots in Git. So, when we save changes in our project, Git takes a picture of how are project looked at that moment and saves a reference to that snapshot. If a file is not changed then Git will not save a new snapshot of that file rather it will link to the previously saved copy of that file.
Most Git operations we perform are done locally and we don't have to stay online or remain connected with any central repository like the one on GitHub. Any changes that we make and save in stored in the local Git repository inside the .git directory.
We only have to connect back to the central repository when we what to sync our local changes in our local Git repository with the central repository.
In Git every change that we save is check-summed and later referenced by that checksum. Git uses SHA-1 hashing algorithm to create the checksum. This hash value is calculated based on the content of a file or directory structure. Checksum is 40 characters long and consists of 0-9 and a-f characters.
There are three main states in Git - modified, committed and staged.
Lets say, we have a project folder (working directory) and we have created a single file and written a line of code. So, the file is currently in modified state as we have not yet saved it in our Git repository.
Once we are done with the changes we move our file from the modified state to the staged state. This means we have marked the changes in the file and are now ready to save the file changes in the local Git repository database.
As long as the file is in staged state, Git allows us to modify the file and re-stage the new changes.
When we are ready to make the final save we move the file from staged state to the committed state which means all the changes done to the file is now saved in our local Git repository database.
When we make a commit then a new snapshot of the file is saved in the Git repository database with a new checksum which we can refer to later and even revert back if needed in future.
If we now make any new change to the file then the file moves back to the modified state.
Have fun learning :-)
Copyright © 2014 - 2022 DYclassroom. All rights reserved.