The official definition of git is "a distributed version control system". But what does it mean? Let's use an example to explain it.
In the next table we have some changes we did to a file:
Time | Status |
---|---|
July 7, 10:00 | Create file tasks.txt. |
July 7, 10:05 | Add paragraph to file tasks.txt. |
July 7, 10:10 | Format the content of tasks.txt. |
… | … |
We can think of every row in the table as a version of the file. So git is a system that helps us track version or changes of files through time (it is also distributed, but that's a feature for another tutorial).
To initialize git inside a folder, we use the command git init. After executing this command, git will start tracking changes inside the folder.
Internally git have three states (also called areas) where changes can be in: working directory, staging area, and repository.
Click the next button to start the animation.
git initThe first thing we need to see git in action, is a file. We can create a new file with the command touch and passing a filename as the first argument (this is a UNIX command not a git one).
This command will create the file tasks.txt. In this file we will keep a list of tasks we need to do.
The working directory area tracks all changes we make inside the folder. So git will show this new file inside this area.
touch tasks.txtA commit is a change, or a group of changes, we wrap together and tell git to save. It is created in 2 steps: the first one is adding all the changes we want to the staging area.
To add changes to the staging area we use the command git add. The next command moves the changes of the file tasks.txt from the working directory to the staging area.
git add tasks.txtBefore, we saw the first of the 2 steps to create a commit.
The second step is explicitly telling git to wrap all the changes currently in the staging area into a commit node. We do this with the command git commit.
We give each commit a message to easily identify its contents later. To pass a message to git commit we use the option –m.
The next command will create our first commit with the message Add file tasks.txt.
git commit –m "Add file tasks.txt"The echo command mixed with >> adds lines to the end of a file.
The first task we want to do is to Buy milk. So we will add it to our file tasks.txt with the echo command.
Look how git tells us that the file has changed by adding it to the working directory area.
echo "Buy milk" >> tasks.txtAnother useful command is git status. This command shows us the changes we have inside the working directory and the staging area.
Look at the output of the command in the console (bottom left). git status will tell us that the changes are not staged (this means that the changes are inside the working directory). All the files with changes inside the working directory will be shown in red. And the file tasks.txt has a modified status because we add a line to it in the last step.
git statusThe command git status just shows us the names of the files with changes. If we want to know what changed inside each file of the working directory, we can use the command git diff.
In the output of git diff, the lines that we added to the file are shown in green, and the ones that we removed are shown in red.
git diffWe want to create another commit with the changes we did to tasks.txt. We need to add the changes to the staging area again with the command git add.
git add tasks.txtWe can use the command git status again to see the message git gives us when we have staged changes. The file tasks.txt still appears with a modified status, but the color has changed to green which means the changes are inside the staging area.
git statusIn step 7 we used the command git diff to see the detailed list of changes we had inside the working directory.
This command can also show the changes inside the staging area, if we pass the option ––cached to it.
The output has the same color code: the lines that we added are shown in green, and the ones that we removed are shown in red.
git diff ––cachedWe want the changes in the staging area to be a new commit. We use the command git commit again, and pass a descriptive message that identifies this commit.
git commit –m "Add first task"The command that completes this tutorial is git log. This command shows us the commits we have made.
git log has various options to show the output with different detail levels. We use the option ––oneline to see the output in a short format.
git log ––onelineThis tutorial has all the basic commands you need to start using git in your personal projects.
There is a lot more to learn about git, look at the Resources section for a list of next steps to expand your knowledge of git.
Also you can use the Cheatsheet section as a reference. It has all the commands in this tutorial with explanations for the most used options.