Starting and adding files to the repository

Starting and adding files to the repository

Git Advanced

Part 1 - Introduction
Basic concepts and working with local repositories

  1. Starting and adding files to the repository
    git init
    git mktree
    git cat-file
    git ls-files
    git checkout
    git read-tree
    git hash-object
  2. Commit
    git config
    git status
    git log
    git commit
    git blame
    git add
  3. Reviewing and staging modified files
  4. Branches
    git checkout or git branch
    git branch -D
  5. HEAD

Part 2 - Remote
Working with remote repositories

  1. Remote Repository
    Remote
    Remote Protocols
    Pushing data to remote repository
    Tracking
    Local Protocol
    SSH Protocol
    Git Protocol
    Script
    Reference
  2. Rebase

Part 3 - Advanced

Tools

  1. Git Cat Index
Remember to cite the source when quoting or paraphrasing information from this website.
Hint

Follow the Steps 1, 2, 3 ... to have a complete understanding.

git init

Create a new empty local GIT repository in the path.

git init  `<path>`
Try it! Step 1

It sets up the necessary files and data structure for start managing your project's version control

git init .

Git Advanced - part 1 2024-01-19 12.12.25.excalidraw.png

Pasted image 20240119120839.png

Low-level commands

git hash-object
echo "Hello, World!" | git hash-object --stdin
echo "Hello, World!" | git hash-object --stdin -w

Pasted image 20240114121254.png
If -w option was selected, a new object will be added to the Git Object Structure (a folder and a file). The first 2 characters will correspond to the name of the subfolder on ./git/objects.

Pasted image 20240114121751.png

(Folder Name) + *ile Name) = SHA1 Hash of the file fixed-size length.
Hash is created based on the content of the file.

hash types

MD5 128 bit (16 bytes)
SHA1 160 bit (20 bytes) 40 hexadecimal characters
SHA 256 bit (32 bytes)
SHA 384 bit (48 bytes)
SHA 512 bit (64 bytes)

GIT uses SHA1 Hash (160 bits).

Shasum

The shasum provides a way to compute SHA1 message.

echo -n "Hello, world!" | shasum

e02aa1b106d5c7c6a98def2b13005d5b84fd8dc8

git cat-file

Output the contents or other properties such as size, type of one or more objects in the repository.

git cat-file -p <hash>
git cat-file -s <hash>
git cat-file -t <hash>

Objects

Git Advanced - part 1 2024-01-19 11.50.44.excalidraw.png

Git is a content-addressable file system. It means at its core Git is a simple key-value data store.

Blobs

Blobs are employed to store files with any extension such as source-code, pictures, text-files or any chunk of data.

Warning

There's a common belief that I've heard so many times that a commit is a diff or a delta from the previous commit. This is a misconception. Git stores all the content in the form of files ./git/objects folder.

git hash-object

Create an object from a file on repository, without a filename. A filename is recorded on the [[#Tree Objects]].

git hash-object <filename> 
Try it! Step 2

Shell script to create blob object.
Pay attention, a physical file is not currently necessary to create an object. In this example, I use the command echo and pipe to combine the output and the next command git hash-object. It will be necessary to give a filename to this blob only in the future when we create a record on the tree file.

--stdin take input from standard input
-w write the git object on the database

echo "Hello Isabela!" | git hash-object --stdin -w

Git Advanced - part 1 2024-01-19 12.28.12.excalidraw.png

What will happen ?

Blob Object Content and hash

Git basic usage 2024-01-17 22.59.27.excalidraw.png

Size Length of content plus one

Tree Objects

A tree is the conceptual representation of a directory in Git. They are employed to keep the filename and group files in directories.

Try it! Step 3

Shell script to create tree file

echo -e "100644 blob a7e68e8fea8c81d43056714b817c232f45835765\ttesting.txt" > tree.txt

Git basic usage 2024-01-18 10.08.43.excalidraw.png

Keeps information about the directories, such as permission, hash and the filename or folder name. It may contain blobs or other trees.

Git Object permission

Git basic usage 2024-01-18 00.10.23.excalidraw.png

git mktree

Reads standard input in a non-recursive ls-tree output format and creates a tree object. But it doesn't move automatically to the staging area.

Try it! Step 4
cat tree.txt | git mktree

Git Advanced - part 1 2024-01-19 14.33.54.excalidraw.png

The number that comes below f5e9c425233d0c38b2f892347ef9a8e5a41bc336
is the sha(1) hash of the tree object. If you want to check it, just type git cat-file <hash>.

Try it! Step 5
git cat-file -t f5e9c

Pasted image 20240118153048.png

Try it! Step 6

You can also see the content using git cat-file -p.

git cat-file -p f5e9c

Pasted image 20240118153601.png

git ls-files

List files in the staging area.

Try it! Step 7

The result bill be empty. The reason we do not move the files present on the git repository to the staging area. To achieve that, letÂŽs do the next command git read-tree using the hash generated on the step before.

git ls-files  -s

Pasted image 20240118132445.png

Git Advanced - part 1 2024-01-18 21.30.49.excalidraw.png

git read-tree

According Git-doc, this command moves* files from Git Repository to the staging area. (Actually the file is copied and converted into a binary representation in a single file called index).

Try it! Step 8
git read-tree f5e9c425233d0c38b2f892347ef9a8e5a41bc336

Git Advanced - part 1 2024-01-19 15.12.43.excalidraw.png

Try it! Step 9

If you repeat the step 4, the file will be available on the staging area

git ls-files  -s

Pasted image 20240118133036.png

Checkout

Try it! Step 10

Now list all files on the root folder.

Pasted image 20240118133359.png

Delete all of them, we are recreating it just for learning with the next command git checkout

Pasted image 20240118133517.png

Try it! Step 11

Now list all files again on the root folder.

Pasted image 20240118150801.png

All the files have been deleted on the step before. We are recreating it with the command git checkout

git checkout

Try it! Step 12
git checkout-index -a 
Try it! Step 13

The file testing.txt was recreated !

Pasted image 20240118151936.png

Git Advanced - part 1 2024-01-19 15.57.39.excalidraw.png

Script to create the example repository

# Step 1 - Create git repository
git init .
# Step 2 - Create blob object
echo "Hello Isabela!" | git hash-object --stdin -w
# Step 3 - Tree object file
echo -e "100644 blob a7e68e8fea8c81d43056714b817c232f45835765\ttesting.txt" > tree.txt
# Step 4 - Create tree
cat tree.txt | git mktree
# Step 5 
git cat-file -t f5e9c
# Step 6
git cat-file -p f5e9c
# Step 7
git ls-files  -s
# Step 8 - Move files  Git Repository to the staging area.
git read-tree f5e9c425233d0c38b2f892347ef9a8e5a41bc336
# Step 9
git ls-files  -s
ls *
rm *
git checkout-index -a 

Starting and adding files to the repository

References

https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
https://github.com/yoichi/git-cat-index/blob/master/git_cat_index.py
https://timestamp.online/