Jorge
Martin
Grace
Download for free: https://git-scm.com/book
Authors:
Scott Chacon,
Ben Straub
Control Version System
Did you...
controlled the versions using the filename?
my_awesome_project.zip
my_awesome_project2.zip
my_awesome_project_final_version.zip
my_not_awesome_project.zip
my_awesome_este_si_es.zip
my_awesome_rosita_de_guadalupe.zip
Control Version System
Did you...
wanna see a previous version of you project?
BUT, YOU CAN'T!
Control Version System
Did you...
lose files and didn't have a backup?
Control Version System
Did you...
share your project using Email, Drive, Whatsapp, Messenger, Photo, etc?
Control Version System
Did you...
merge your work with your teammates work?
Control Version System
"Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency".
Definition taken from https://git-scm.com/
Your project
Working directory/area
index
staging area
Your branch
Working branch
? ? ?
? ? ?
committed
modified
staged
Your file is...
You are in...
# Local (or project) scope
$ git config user.name “Jorge Rios”
$ git config user.email “jrios@utec.edu.pe”
# Global scope
$ git config --global user.name “Jorge Rios”
$ git config --global user.email “jrios@utec.edu.pe”
$ git config --list
$ git init
Initialized empty Git repository in {PATH}/.git/
master *
$ git add --all
master *
$ git commit -m "Add my text on README.md"
master *
$ git status
Your project
Working directory/area
index
staging area
Your branch
Working branch
git add
You are in...
git commit
Your project
Working directory/area
index
staging area
Your branch
Working branch
git add
You are in...
git commit
origin/master
origin
Remote repository
git push
Your project
Working directory/area
index
staging area
Your branch
Working branch
You are in...
origin/master
origin
Remote repository
git pull
$ git diff
$ git commit -- {file}
See my changes
Unmodifying a modified file in the working directory
$ git reset HEAD -- {file}
Revert staged changes
master *
$ git checkout -b feature/blizzard-logo
master *
$ git checkout feature/blizzard-logo
feature/blizzard-logo *
$ git branch -d feature/blizzard-logo
Create a new branch
Use a branch
Delete a branch
https://bit.ly/repolink
$ git clone https://github.com/gracenikole/py-project.git
Cloning 'py-project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 2 (delta 0), pack-reused 3
Receiving objects: 100% (6/6), done.
master*
$ git branch develop
master*
$ git checkout develop
Changed to branch 'develop'
and add your name in README.md file
master*
$ git checkout -b develop
Changed to branch 'develop'
develop*
$ git branch
* develop
master
develop*
$ git add --all
divide*
$ git commit -m "Update {readme.md}"
develop*
$ git checkout master
Changed to branch 'master'
master*
$ git merge develop
Updating a3d68e0..656ec87
Fast-forward
README.md | 10 +++++++++-
master*
$ git branch -d develop
Deleted branch develop (656ec87)
master*
$ git push
$ git init
Initialized empty Git repository in {PATH}/.git/
Initialize a new Git repository
$ tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── ...
│ ├── pre-receive.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
$ vim testing_blob.py
What is a Git object?
# file: testing_blob.py
print("Hello world from Git")
$ git hash-object -w testing_blob.py
5eefe3945ef60476ef9a4f37f4f3d653ef2316fe
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
| ├── ...
│ ├── pre-receive.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── 5e
│ │ └── efe3945ef60476ef9a4f37f4f3d653ef2316fe
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
$ git cat-file -p 5eefe3945ef60476ef9a4f37f4f3d653ef2316fe
print("Hello world from Git")
$ git cat-file -t 5eefe3945ef60476ef9a4f37f4f3d653ef2316fe
blob
Discovering the git object
Retrieving the content
So far we have seen only the "front end" of Git. These are called the porcelain commands. Now it's time to look at the plumbing, the part that does all the heavy lifting
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
| ├── ...
│ ├── pre-receive.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── 5e
│ │ └── efe3945ef60476ef9a4f37f4f3d653ef2316fe
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
We will need to define some concepts before moving on to really understand how each part of Git works
We will consider an object to be a piece of data.
Ex. a .txt file, a .png image.
They "live" in a location
A pointer is an object. But its data always contains a location of another object
An index is a special type of metadata.
It will contain important locations within a database
A hash is a mathematical transformation defined as f(x), where x is some object.
This transformation convert the object of length n into a constant length series of hexadecimal digits
#See content
git cat-file -p 6f2751951abc19u98
#Create object
git hash-object -w main.cpp
#See tree
git cat-file -p 6abfe790271bd
#Add pointer to blob
git update-index --add --cacheinfo 100644 82142174bacd main.cpp
#Save tree
git write-tree
#Add pointer to tree
git read-tree --prefix=<tree_alias> 6abfe790271bd
# Commit a tree
git commit-tree abcd1234 -m "first commit"
# Or with a pipe
echo "first commit" | git commit-tree abcd1234
#Commit a tree with a parent
git commit-tree abcd5678 -p abcd1234 -m "second commit"
Ex.
ref: refs/heads/master
Stores the heads information of different remote repositories.
Ex.
.git/refs/remotes/origin/master
.git/refs/remotes/fork/master
They are considered read-only since they get updated on push/pull
Ex. Beta, ver0.1, etc
Lives in ./git/refs/tags
If I change a ; in a file with 10 million lines a new file gets, created. Could we store only the change while keeping our structure ?
#Converts objects into packs
git gc
# See HEAD changelog
git reflog
# See data integrity
git fsck --full
Get for free from https://education.github.com/pack