Git Essentials(Second Edition)
上QQ阅读APP看书,第一时间看更新

Git objects

In Chapter 1, Getting Started with Git, we created an empty folder (in C:\Repos\MyFirstRepo) and then we initialized a new Git repository, using the git init command.

Let's create a new repository to refresh our memory and then start learning a little bit more about Git.

In this example, we use Git to track our shopping list before going to the grocery; so, create a new grocery folder, and then initialize a new Git repository:

[1] ~
$ mkdir grocery

[2] ~
$ cd grocery/

[3] ~/grocery
$ git init
Initialized empty Git repository in C:/Users/san/Google Drive/Packt/PortableGit/home/grocery/.git/

As we have already seen before, the result of the git init command is the creation of a .git folder, where Git stores all the files it needs to manage our repository:

[4] ~/grocery (master)
$ ll
total 8
drwxr-xr-x 1 san 1049089 0 Aug 17 11:11 ./
drwxr-xr-x 1 san 1049089 0 Aug 17 11:11 ../
drwxr-xr-x 1 san 1049089 0 Aug 17 11:11 .git/ 

So, we can move this grocery folder wherever we want, and no data will be lost. Another important thing to highlight is that we don't need any server: we can create a repository locally and work with it whenever we want, even with no LAN or internet connection. We only need them if we want to share our repository with someone else, directly or using a central server.
In fact, during this example, we won't use any remote server, as it is not necessary.

Go on and create a new README.md file to remember the purpose of this repository:

[5] ~/grocery (master)
$ echo "My shopping list repository" > README.md

Then add a banana to the shopping list:

[6] ~/grocery (master)
$ echo "banana" > shoppingList.txt

At this point, as you already know, before doing a commit, we have to add files to the staging area; add both the files using the shortcut git add .:

[7] ~/grocery (master)
$ git add .

With this trick (the dot after the git add command), you can add all the new or modified files in one shot.

At this point, if you didn't set up a global username and email like we did in Chapter 1, Getting Started with Git, this is a thing that could happen:

[8] ~/grocery (master)
$ git commit -m "Add a banana to the shopping list"
[master (root-commit) c7a0883] Add a banana to the shopping list
Committer: Santacroce Ferdinando <san@intre.it>

Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate.

You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author

2 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 shoppingList.txt

First of all, take a look at the second line, where Git says something like root commit; this means this is the first commit of your repository, and this is like a root in a tree (or a root on a disk partition; maybe you nerds will understand this better). Later we will come back to this concept.

Then, Git shows a message that says: "You didn't set a global username and email; I used ones I found configured in your system, but if you don't like it, you can go back and remake your commit with another pair of data".

I prefer not to set up a global username and password in Git, as I usually work on different repositories using different usernames and emails; if I don't pay attention, I end up doing a job commit with my hobby profile or vice versa, and this is annoying. So, I prefer setting up usernames and emails per repository; in Git, you can set up your config variables at three levels: repository (with the --local option, the default one), user (with the --global option), and system-wide (with the --system option). Later we will learn something more about configuration, but this is what you need for now to go on with.

So, let's change these settings and amend our commit (amending a commit is a way to redo the last commit and fix up some little mistakes, such as adding a forgotten file, changing the message or the author, as we are going to do; later we will learn in detail what this means):

[9] ~/grocery (master)
$ git config user.name "Ferdinando Santacroce"

[10] ~/grocery (master)
$ git config user.email ferdinando.santacroce@gmail.com

As I didn't specify the config level, these parameters will be set at repository level (the same as --local); from now on, all the commits I will do in this repository will be signed by "Ferdinando Santacroce", with the ferdinando.santacroce@gmail.com email (now you know how to get in touch with me, just in case).

Now it's time to type this command, git commit --amend --reset-author. When amending a commit this way, Git opens the default editor to let you change even the commit message, if you like; as we have seen in Chapter 1, Getting Started with Git, in Windows the default editor is Vim. For the purpose of this exercise, please leave the message as it is, press Esc, and then input the :wq (or :x) command and press Enter to save and exit:

[11] ~/grocery (master)
$ git commit --amend --reset-author #here Vim opens
[master a57d783] Add a banana to the shopping list
 2 files changed, 2 insertions(+)
 create mode 100644 README.md
 create mode 100644 shoppingList.txt

Okay, now I have a commit with the right author and email.