Git Essentials - Sample Chapter
Git Essentials - Sample Chapter
P U B L I S H I N G
pl
$ 24.99 US
16.99 UK
Sa
m
C o m m u n i t y
Ferdinando Santacroce
Git Essentials
Git Essentials
ee
E x p e r i e n c e
D i s t i l l e d
Git Essentials
Create, merge, and distribute code with Git, the most powerful
and flexible versioning system available
Forewords by Arialdo Martini, Solutions Architect at Aduno Gruppe
Nicola Paolucci, Developer Advocate at Atlassian
Dr. Andr Roberge, Former President and Vice-Chancellor
of Universit Sainte-Anne
Ferdinando Santacroce
Git Essentials
If you are reading this book, you are probably a software developer and a professional.
What makes a good professional? Sure, culture and experience are a part of the answer,
but there's more: a good professional is one who can master different tools, choose the
best tool for the job at hand, and has the necessary discipline to develop good
working habits.
Version control is one of the base skills for developers and Git is one of the right tools
for the job. However, Git can't be compared to a screwdriver, a simple tool with only a
basic function; Git provides a complete toolbox that can help you manage your own
code, within which there are also other sharp tools that should be handled with caution.
The final aim of this book is to help you to start using Git and its commands in the safest
way possible, to get things done without causing any injuries. Having said this, you will
not get the most from Git commands if you do not acquire the right habits; just as is the
case with other tools, in the end, it is the craftsman who makes all the difference.
This book will cover all the basic topics of Git, thereby letting you start using it even if
you have little or no experience with versioning systems; you only need to know about
versioning in general and the Wikipedia-related page is enough for this purpose.
Chapter 5, Obtaining the Most Good Commits and Workflows gives you some hints
about common methods you can use to organize the source code within Git, thereby
helping you to develop good habits that every developer should possess.
Chapter 6, Migrating to Git is a way to give you a hand if you're used to other
versioning systems, such as Subversion, to manage the transitional phase from
another piece of software to Git.
Chapter 7, Git Resources offers you some hints, which I've taken from my experience,
and could prove interesting for you.
[1]
Installing Git
Git is open source software. If you are running a Windows machine, you can
download it for free from http://git-scm.com. At the time of writing this book,
the current version of Git is 1.9.5. If you are a Mac user, you can download it from
http://git-scm.com/downloads; here, you can find the *.dmg file, too. Finally,
if you are a Linux user, you probably have Git out of the box (if not, use the aptget install git command or equivalent). Here, I won't go into too much detail
about the installation process itself; I will only provide a few recommendations for
Windows users shown in the following screenshot:
[2]
Chapter 1
[3]
Use defaults for line endings. This will protect you from future annoyances.
At the end of the process, we will have Git installed, and all its *nix friends will be
ready to use it.
[4]
Chapter 1
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!
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?
[5]
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.
[6]
Chapter 1
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.
[7]
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.
[8]
Chapter 1
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!
[9]
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:
[ 10 ]
Chapter 1
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.
Summary
In this chapter, you learned that Git is not so difficult to install, even on a non-Unix
platform such as Windows.
Once you have chosen a directory to include in a Git repository, you can see that
initializing a new Git repository is as easy as executing a git init command, and
nothing more. Don't worry now about saving it on a remote server and so on. It's not
mandatory to save it; you can do this when you need to, preserving the entire history
of your repo. This is a killer feature of Git and DVCS in general. You can comfortably
work offline and push your work to a remote location when the network is available,
without hassle.
In the end, we discovered one of the most important character traits of Git: it will
do nothing if you don't mention it explicitly. You also learned a little bit about the
add command. We were obliged to perform a git add command for a file when we
committed it to Git the very first time. Then, we used another command when we
modified it. This is because if you modify a file, Git does not expect that you want it
to be automatically added to the next commit (and it's right, I'll say).
In the next chapter, we will discover some fundamentals of Git.
[ 11 ]
www.PacktPub.com
Stay Connected: