Book Image

Git Essentials

By : Ferdinando Santacroce
Book Image

Git Essentials

By: Ferdinando Santacroce

Overview of this book

Table of Contents (17 chapters)

Running our first Git command


Now, we have to test our installation. Is Git ready to rock? Let's find out!

Open a prompt and simply type git (or the equivalent, git --help), as shown in the following screenshot:

If Git has been installed correctly, typing git without specifying anything else will result in a short help page, with a list of common commands. If not, try reinstalling Git, ensuring that you have checked the Use Git from the Windows Command Prompt option. Otherwise, Git will be available only within the embedded Bash shell.

So, we have Git up and running! Are you excited? Let's begin to type!

Setting up a new repository

The first step is to set up a new repository (or repo, for short). A repo is a container for your entire project; every file or subfolder within it belongs to that repository, in a consistent manner. Physically, a repository is nothing other than a folder that contains a special .git folder, the folder where the magic happens.

Let's try to make our first repo. Choose a folder you like, and type the git init command, as shown here:

Whoa! What just happened? Git created a .git subfolder. The subfolder (normally hidden in Windows) contains some files and folders, as shown in the next screenshot:

At this point, it is not important for us to understand what is inside this folder. The only thing you have to know is that you do not have to touch it, ever! If you delete it or if you modify files inside by hand, you could get into trouble. Have I frightened you enough?

Now that we have a repo, we can start to put files inside it. Git can trace the history of any gender of files, text based or binary, small or large, with the same efficiency (more or less, large files are always a problem).

Adding a file

Let's create a text file just to give it a try.

And now what? Is that all? No! We have to tell Git to put this file in your repo, explicitly. Git doesn't do anything that you don't want it to. If you have some spare files or temp ones in your repo, Git will not be compatible with them, but will only remind you that there are some files in your repo that are not under version control (in the next chapter, we will see how to instruct Git to ignore them when necessary).

Ok, back to the topic. I want MyFile.txt under the control of Git, so let's add it, as shown here:

The git add command tells Git that we want it to take care of that file and check it for future modifications.

Has Git obeyed us? Let's see.

Using the git status command, we can check the status of the repo, as shown in the following screenshot:

As we can see, Git has accomplished its work as expected. In this image, we can read words such as branch, master, commit and unstage. We will look at them briefly, but for the moment, let's ignore them.

Commit the added file

At this point, Git knows about MyFile.txt, but we have to perform another step to fix the snapshot of its content. We have to commit it using the appropriate git commit command. This time, we will add some flavor to our command, using the --message (or -m) subcommand, as shown here:

Press the Enter key.

Feel the magic—a new branch is born!

With the commit of MyFile.txt, we have finally fired up our repo. It now has a master branch with a file within it. We will play with branches in the forthcoming chapters. Right now, think of it as a course of our repository, and keep in mind that a repository can have multiple courses that often cross each other.

Modify a committed file

Now, we can try to make some modifications to the file and see how to deal with it in the following screenshot:

As you can see, Bash shell warns us that there are some modifications painting the name of the modified files in red. Here, the git status command informs us that there is a file with some modifications and that we need to commit it if we want to save this modification step in the repository history.

However, what does no changes added to commit mean? It is simple. Git makes you take a second look at what you want to include in the next commit. If you have touched two files but you want to commit only one, you can add only that one.

If you try to do a commit without skipping the add step, nothing will happen. We will see this behavior in depth in the next chapter.

So, let's add the file again for the purpose of getting things ready for the next commit.

Let's do another commit, this time, avoiding the --message subcommand. So, type git commit and hit the Enter key.

Fasten your seatbelts! You are now entering into a piece of code history!

What is that? It's Vim (Vi Improved), an ancient and powerful text editor. You can configure Git to use your own preferred editor, but if you don't do it, this is what you have to deal with. Vim is powerful, but for newcomers, it can be a pain to use. It has a strange way of dealing with text, so to start typing, you have to press i for inserting text, as shown in the following screenshot:

Once you have typed your commit message, you can press Esc to get out of the editing mode. Then, you can type the :w command to write changes and the :q command to quit. You can also type the command in pairs as :wq.

After that, press Enter and another commit is done, as shown here:

Note that when you saved the commit message in Vim, Git automatically dispatches the commit work, as you can see in the preceding screenshot.

Well done! Now, it's time to recap.