Tutorials
Software Tools
Git
INTRODUCTION
This is a short tutorial using a nice visual representation to show how to use Git, a version control system. It will first show the basics for an individual project, and then for a small group project.
The prerequisites are that you need to be familiar with the command line or terminal, and are familiar with project development, and have Git installed.
Why Git?
Let's say you were working on a project in your spare time, and you want to try out something new with what you have made, but want to make sure that your current version is safe in case you make too many changes. What do you do? Most likely, you will simply copy all of your files and save them as a backup, so that you can keep modifying the original files.
This is what any version control system can provide you.
So, why use Git if any version control system provides me with this functionality?
What Git offers is the ability to have a well-defined context in which your work will be done, and the ability to switch contexts in order to have multiple possible implementations to be tested. The concept of context is very important to Git, as I will mention later. Git does not need a 'host' server for keeping the files. One computer is all that is necessary for keeping track of all of the changes that have been ever made to the file.
In any project you work on, you want to take one step at a time with it. Git meshes very well with this work habit. The best way to use Git is to simply make one change (e.g. add a functionality, remove a bug, improve one performance issue), and save that as a newer version of your entire file, providing a concise description of what changes you have done (after testing of course).
Each time you make a solid change to the files, and want to save that, you let Git know what files are the ones you want to save, and then tell Git to save the state of the project with the changes made to the specified files. Effectively, you are taking a snapshot of the entire directory, and only the files whose changes you want to save, are in that snapshot. You tell Git which files are going to be in the snapshot, and then you take the snapshot.
One neat concept in Git is the concept of an Index (also called Stage). The index contains all the files that are going to be within the snapshot. When the files are added to the stage, further modifications of the files will not be included in the staged versions of them! This is very important because it allows files to be modified before the stable versions are taken a snapshot.
That's all for the basics of Git for an individual!
- Make changes to the files in a project
- Let Git know of the important changes
- Tell Git to save the new state of the directory
What happens if you want to revert back to a previous version? There are several approaches available.
- Revert the entire directory the last saved state.
- Revert one file to what it was in the last saved state.
- More advanced versions available which control which save is used, and how many files are changed.
Now onto the actual commands!
In your project folder, you want to initialize the Git system so that it will keep track of all of the changes made to the files existing in the project. This is all done using the command line or terminal.
$git init
Next, you want to add all of your files in your project folder to keep track of them all (since git only saves the files you say are modified). (If you are working in an empty directory, make a simple README file to test this functionality out) What files are new? Use the following command to see.
$git status
This command displays the status of the directory. Specifically, it shows which files are ready to be saved, and which files are new or modified but are not noticed by Git yet. The "Changes to be committed:" section displays the files that would be saved in the new "snapshot", and the "Changes not staged for commit:" shows the files that have been changed from the last save, but are not going to be saved unless you tell it to remember those changes.
How do you tell Git to remember the changes?
$git add [filename]
This command adds the file specified to the "picture to be taken". You can add all the files in this directory by using the following command:
$git add .
To finally take the full snapshot, you execute the following command.
$git commit
Of course, when taking snapshots, a small comment is crucial so that it makes sense to others when they see them.
$git commit -m "Message"
And that is all for Git! There are several shortcuts you can use, but this is the gist of Git, and once you understand this, you can use the shortcuts easily. Now onto reverting files.
$git reset --hard HEAD
Reverts everything to the last commit. Nothing in the index is saved. HEAD is the variable storing the latest commit.
$git checkout -- [filename]
If an older version exists in the index, this will restore the file from the index.
$git checkout HEAD [filename]
Reverts the file from the latest commit.
Individual User Guide
| Concept | Command | Metaphor |
|---|---|---|
| Notifying wanted changes | $git add [filename] |
Bringing people together for a snapshot |
| Saving all the wanted changes | $git commit -m "Message" |
Taking a snapshot and saving it |
| Reverting to saved version of entire directory | $git reset --hard HEAD |
Recalling everyone from a previous snapshot |