You’re finally ready to get started with Git! In this chapter, you’ll be learning a few Git terminologies and concepts necessary for any project. Then, you’ll be tasked to set up a project, make changes to it, review the changes, and finally navigate between versions. Let’s go!
Repositories
A repository is a storage where all your project and all the changes made to it are kept. You can think of it as a “change database.” But don’t worry; it is only a normal folder on your system, so it is very easy to manipulate.
For each project you want to manage with Git, you have to set up a repository for it. Setting up a repository is very easy. Just navigate to the folder you want to track and tell Git to initiate a repository there.
Create the directory containing your project
Navigate into the directory
Initialize a Git repository
See? It’s very easy. Let’s convert those statements into commands. But first, let’s open a console to type our commands in. For Linux users, you just have to launch your favorite terminal (Ctrl-Alt-T for Debian like distros). For MacOS, you just have to use Cmd-Space to bring up Spotlight where you can search for the Terminal app. Windows users can open two consoles: cmd and powershell. Powershell is more modern and has UNIX-like commands. To open one of them, use Windows-R and type in the name (cmd or powershell). Note that you need to restart all these consoles on your first installation of Git if you had them open. Git for Windows also comes with a console emulator called Git Bash that provides a similar environment to Linux and Mac consoles. If you use Windows, I highly suggest to use Git Bash so you can have the same experience as other people who use different OSs.
mkdir is a command used to create a directory; it is short for “make directory.” cd is the command used to navigate between directories; it is short for “change directory.” Finally, git init is short for “Git initialize.”
Note
mkdir and cd are system commands; they are managed by the OS, whereas init is a Git command. Every Git command begins with “git.”
Remember Chapter 1 that said that instead of tracking changes between versions, Git takes snapshots? Well, all those snapshots are stored in the “.git” directory. Each snapshot is called “commit,” and we’ll look into that shortly after this section.
The HEAD file in this “.git” directory points to the current “branch” or subversion of the project that you are working on. The default branch is called “master,” but it is just like any other branch; the name is just an old convention.
You should also know that initializing is the only way to get a repository. You can copy an entire repository with all its history and snapshots. It is called “cloning,” and we will see that in another chapter.
EXERCISE: CREATE AN EMPTY REPOSITORY
Our first exercise isn’t exactly rocket surgery. Just create an empty repository somewhere in your system. You can use the default console or Git Bash.
Working Directory
What about the empty area outside the “.git” directory? It is called the Working Directory, and the files you will be working on will be stored there. Generally, your most recent version will be on the Working Directory.
Each file you work on is on the Working Directory. There is nothing particular about this place except the fact that you will only manipulate the files here directly. Never modify the files inside the “.git” directory!
If we check the status of the Working Directory, we will get a result like the one shown in Figure 3-5.
Essentially, that is the Working Directory: the area where you directly interact with your project files.
EXERCISE: CREATE SOME FILES FOR THE PROJECT
This exercise is again very easy. Just create some files within your project directory (repository) and check the Working Directory status.
Staging Area
The Staging Area is where your files go before the snapshots are taken. Not every file you modified on the Working Directory should be taken into account when taking a snapshot of the current state of the project. Only the files placed in the Staging Area will be snapshotted.
So, before taking a snapshot of the project, you select which changed files to take account of. A change in a file can be creating, deleting, or editing.
It’s that simple. If we wanted to stage the README.md that we created earlier, we would use “git add README.md.” Or if you created multiple files, you can add them one after another or together like “git add file1 file2 file3.”
If you check out Figure 3-6, you will notice that after staging the file, the Working Directory is clean again. That’s because “git status” only keeps track on “unstaged” files (edited files that have not been marked for a snapshot).
Caution
Don’t forget the option “--cached” when unstaging a file. If you forget it, you could lose your file!
After you stage all the files that you want the changes to be taken into account, you are now ready to take your first snapshot!
EXERCISE: STAGE AND UNSTAGE YOUR FILES
Take the files you created on the previous exercise and stage them. Unstage one file and re-stage it. Check the Working Directory status after each stage/unstage.
Commits
Like we talked about in the section before this one, a commit is just a snapshot of the entire project at a certain time. Git doesn’t record the individual changes done to the files; it takes a picture of the entire project.
In addition to the snapshot, a commit also contains information about the “author” of the content and the “commiter” or who put the changeset into the repository.
Note
“author” and “commiter” are usually the same person, unless the commiter took the changeset from another team member. Remember that Git commits are exchangeable since it is a distributed VCS.
Since a commit is a snapshot from the state of the project, the previous state of the project is another commit called “parent.” The very first commit is created by Git when the repository is created, and it’s the one commit that has no parents. All future commits are then linked to each other via parentage. The ensemble of those commits that are parents to each other is called “branch.”
Note
If a commit has two parents, that means that it was created by merging two branches.
A commit is identified by its name, a 40-character string that is obtained by hashing the commit. It is a simple SHA1 hash so multiple commits with the same information will have the same name.
A reference to a specific commit is called “head,” and it also has a name. And the head you are currently working on is called “HEAD” (see the previous section).
We can now commit the files we staged earlier. Before each commit, you should check the status of the Working Directory and the Staging Area. If all the files you want to commit are in the Staging Area (under the phrase “Changes to be committed”), you can commit. If not, you have to stage them with “git add.”
If we execute this command, it will open our default editor (check Chapter 2 if you want to modify yours) and ask us for a commit message. A commit message is a short description of what has changed in the commit compared to the previous one.
You can see in Figure 3-7 that the first line of the file is empty; that’s where you have to write the commit message. The commit message should be written on one line, but you can always add more lines of comments. Comments start with “#” and are ignored by Git; they are only used to complete the commit message, to make it clearer. Also note that Git puts automatically the list of changed files in the commit comments (the same files you saw with “git status”).
The current branch: master
The name of the previous commit: root-commit because this is our first commit
The name of the commit: the first seven letters of the commit hash
The commit message
The number of files changed: one file
The operation done to each file: creation
We took our first snapshot! If you check the status of the repository, you can see that it is clean again, unless you left some files unstaged.
EXERCISE: COMMIT YOUR CHANGES
Take your staged files from the previous exercise and commit them. Then modify one of your tracked files, stage it again, and make a new commit. Compare the summary of each commit. What is different? In what way are those commits linked?
Quick start with Git
So, now that you are familiar with the basic concept of Git, we are going to apply them in a real project. Let’s imagine you want to create a folder to hold your TODO list and want it to be versioned so you can check when each item was completed.
To get you more familiar with Git, you will be doing the next exercise without any help. If you get stuck, just check the previous sections for directions.
You modify the files on the Working Directory.
You put the files you want to record the current state on the Staging Area.
You take a snapshot of the project with a commit.
Don’t forget to put the files you modified on the Staging Area before committing or they won’t be part of the snapshot. The modified files you didn’t put on the Staging Area will just stay on the Working Directory until you decide to discard them or include them in a future commit.
Let’s get started on the exercise! Please complete it until the end and don’t move on to the next chapter until you understand clearly how Git works.
EXERCISE: A VERSIONED TODO APP
Create a new repository.
Create a file named TODO.txt in the directory and put in some text.
Stage TODO.txt.
Commit the project and put in a short commit message.
Create two new files named DONE.txt and WORKING.txt.
Stage and commit those files.
Rename WORKING.txt to IN PROGRESS.txt.
Add some text to DONE.txt.
Check the directory status.
Stage IN PROGRESS.txt and DONE.txt.
Unstage DONE.txt.
Commit the project.
Check the directory status.
Working Directory
Staging Area
Commit
If you don’t have too many problems understanding those concepts, you are ready for more Git commands and concepts.
Summary
Modified: You modified a file on the Working Directory.
Staged: You added the file to the Staging Area so it could be snapshotted.
Committed: You took a snapshot of the entire project (all the unmodified and staged files).
If a file was part of the previous commit and you didn’t modify them, they will automatically be part of the next commit. A modified but unstaged file is considered as unmodified. You have to ask Git to track them by staging those files.
We also learned a little bit about committing and commit messages. Opening an external editor to write commit messages might be a little awkward at first, but you will eventually get the hang of it after some time.
In the next chapter, we will learn how to check the project history and navigate between versions. We will also learn about ignoring certain files and show the current changes done to the project since the last commit.