Reader small image

You're reading from  Git Essentials

Product typeBook
Published inApr 2015
Reading LevelBeginner
Publisher
ISBN-139781785287909
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Ferdinando Santacroce
Ferdinando Santacroce
author image
Ferdinando Santacroce

Ferdinando Santacroce is a developer, author, and trainer who loves learning new things. As a software developer, Ferdinando has mainly worked on the .NET platform using C#, bridging the gap between old-style systems and new technologies. Over the span of his career, he has allowed some COBOL applications to talk to remote services, databases, and electronic devices such as cash handlers, scanners, and electronic shelf labels. At the moment, he is committed to helping the largest energy player in Italy to face new challenges in the market by developing Java and JavaScript-based applications. Other than this, he's focusing on continuous improvement and agile movement, which he follows with great care, in conjunction with XP foundations and lean manufacturing. He's one of the organizers of Italian Agile Days, the most famous and appreciated conference in the Italian Agile panorama. Ferdinando loves to share ideas with other professionals and to speak at public conferences; every time he has, he has learned something new. He enjoys writing as well. After a hiatus of a few years, he has started blogging again about his work and passions, which, according to him, are more or less the same thing. Most of what he has learned over the years has been with the help of his friends and colleagues. Other than working within the same team or on the same code base, they have encouraged him to read books and attend inspiring conferences, such as XP Days, Italian Agile Days, and others that have helped his growth.
Read more about Ferdinando Santacroce

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
Git Essentials
Published in: Apr 2015Publisher: ISBN-13: 9781785287909
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ferdinando Santacroce

Ferdinando Santacroce is a developer, author, and trainer who loves learning new things. As a software developer, Ferdinando has mainly worked on the .NET platform using C#, bridging the gap between old-style systems and new technologies. Over the span of his career, he has allowed some COBOL applications to talk to remote services, databases, and electronic devices such as cash handlers, scanners, and electronic shelf labels. At the moment, he is committed to helping the largest energy player in Italy to face new challenges in the market by developing Java and JavaScript-based applications. Other than this, he's focusing on continuous improvement and agile movement, which he follows with great care, in conjunction with XP foundations and lean manufacturing. He's one of the organizers of Italian Agile Days, the most famous and appreciated conference in the Italian Agile panorama. Ferdinando loves to share ideas with other professionals and to speak at public conferences; every time he has, he has learned something new. He enjoys writing as well. After a hiatus of a few years, he has started blogging again about his work and passions, which, according to him, are more or less the same thing. Most of what he has learned over the years has been with the help of his friends and colleagues. Other than working within the same team or on the same code base, they have encouraged him to read books and attend inspiring conferences, such as XP Days, Italian Agile Days, and others that have helped his growth.
Read more about Ferdinando Santacroce