1.2 Local repositories and commit#

In this chapter, we will explore the creation of local repositories and the process of committing changes. We will also learn how to interact with Git using the command line and Visual Studio Code (VSC).

Creating a dummy project#

If we want to learn about Git, we need a project to work with. Git can track changes in any type of files, it doesn’t have to be code. So to keep it simple, we will create a dummy project, which will be just a directory with some text files in it.

Test your knowledge

  • Create an empty directory for our project and name it my-project.

  • Change into your project directory.

  • Verify that the directory is empty and that it does not contain any hidden files.

Hide code cell source
mkdir my-project
cd my-project
ls -a

Initialize a repository#

To tell Git that you want it to keep track of your project changes, we need to initialize a Git repository. To do that we use the git init command.

git init -b main
Initialized empty Git repository in /home/callaram/tds/home/ch1/my-project/.git/

What to notice

After initializing the repository:

  • A new hidden directory named .git is created inside the project directory. This directory will contain all the necessary files to keep track of the changes in the project.

In Visual Studio Code (VSC):

  • The main branch is mentioned in the left bottom corner of the window.

  • The .git folder is not visible in the explorer because is hidden by default to avoid accidental changes. You can modify this behavior in the settings.

Test your knowledge

  • Verify that the .git directory was created.

ls -a
.  ..  .git

Know more: Main vs Master branch

From Git 2.28 onwards, the default branch name can be changed by using the command git config --global init.defaultBranch main.

Check the repository status:

git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Deleting the Git repository#

To delete the repository, we can just simply delete the .git folder, we can do this with the rm command followed by the -rf option (to delete all the content without confirmation) and the path to the .git folder:

rm -rf .git

After deleting the repository,all the changes made to the files in the working directory will remain, but the history of the changes will be lost.

If you try this command you will need to initialize the repository again with:

git init -b main
Initialized empty Git repository in /home/callaram/tds/home/ch1/my-project/.git/

The three basic sections of Git#

So we have a directory for our project and a repository (the .git directory) associated to it but how do we actually keep track of the changes in our project? To keep track of the changes, Git uses three areas: the working directory, the staging area, and the repository to allow the user to generate snapshots of the project (that we call commits).

  1. The working directory is the area in which you create, edit and delete files.

  2. The staging area, or index, it is a file, stored in the .git directory, that stores information about what will go into your next commit.

  3. The repository is where git stores all the commits, the snapshots of the content of your working directory that was staged for a commit. Git generates a commit history by linking each commit to previous commits.

Commiting changes#

Test your knowledge

  • Use the terminal to create a new file named file1.txt.

  • Use the terminal to add the text “This is file 1.” to the file.

Hide code cell source
touch file1.txt
echo "This is the first line." >> file1.txt

Now we have a file in the working directory. Let’s check the repository status using the git status command.

git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	file1.txt

nothing added to commit but untracked files present (use "git add" to track)

What to notice

  • Git tells us that the file is untracked.

In VSC:

  • the file appears in the Explorer with a “U” symbol indicating that it is untracked.

The two steps for a commit: add and commit#

To ‘move’ a file from the working directory to the staging area, we use the git add command.

git add file1.txt
git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   file1.txt

What to notice

  • The file is now on the list of ‘changes to be commited’ (in the staging area).

In VSC:

  • the file appears in the Explorer with a “A” symbol indicating that it has been added to the staging area.

To ‘move’ all the changes in the staging area to the repository, we use the git commit command. We also need to add a commit message to describe the changes we made using the -m option.

git commit -m "Create file1.txt with the first line"
[main (root-commit) 2007711] Create file1.txt with the first line
 1 file changed, 1 insertion(+)
 create mode 100644 file1.txt
git status
On branch main
nothing to commit, working tree clean

What to notice

  • Git tells us that the working tree is clean, meaning that the working tree matches the tree of the last commit. All changes have been committed and there are no untracked files. In VSC:

  • The file is no longer emphasized in the Explorer.

Listing the commits#

We can list the commit history using the git log command.

git log
commit 2007711caa39b5ad6bee678519eb0b98e71bd1a3 (HEAD -> main)
Author: msdp-book <msdp.book@gmail.com>
Date:   Fri Mar 15 19:16:17 2024 +0100

    Create file1.txt with the first line

What to notice

  • The commit is now listed.

  • The long string of characters is the commit hash. It’s a unique identifier for the commit automatically generated by Git.

  • The author (that we added to the config) and date of the commit are also listed.

  • The commit message Add file1.txt is also displayed.

A commit behind the scenes#

When we commit changes, Git creates a new object in the .git/objects directory. This object contains the changes we made to the files, the author, the date, and the commit message.

ls -a .git/objects
.  ..  20  59  c0  info  pack