0% found this document useful (0 votes)
44 views

Git and Github

Git is a distributed version control system that tracks changes to files locally and allows for collaboration. GitHub is a hosting service for Git repositories that provides additional features like website hosting. Common Git commands are used to initialize and clone repositories, add and commit changes, and manage branches and commits.

Uploaded by

Games Tames
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Git and Github

Git is a distributed version control system that tracks changes to files locally and allows for collaboration. GitHub is a hosting service for Git repositories that provides additional features like website hosting. Common Git commands are used to initialize and clone repositories, add and commit changes, and manage branches and commits.

Uploaded by

Games Tames
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

GIT AND GITHUB

GIT

 Git is a distributed version control system (collaborated with people and work on the same project)
 Can track changes in files in any folder (repository)
 Git preserves history of all changes in the files
 Git could be used locally without internet and locally you will have full access to the files change
history
 Git stores all changes locally and does not rely on internet connection

HOW DOES IT DO THAT?

 Git has its own file structure (special file system)


 Every file is stored in the separate document and has unique hash
 Git is simply a persistent hash map that stores key-value pairs
 Key is a hash of the file and value is content of the file

GITHUB

 Github is a repository hosting service


 Github is a backup for local repositories
 Github is used primarily by distributed teams that work on the same projects
 Capable of website hosting and desktop use

SHELL

 Mkdir – make directory


 Ls – list files and directories
 Cd – change directory
 . – alias of the current directory
 .. – alias of the parent directory
 nano – edit file
 clear – clear terminal
 tab – autocomplete command
 echo – print to terminal
 man – help on specific command
 pwd – print working directory
 touch – create new files
 > - write to the file
 >> - append to the file
 Cat – list contents of the file
 Rm – remove files and directories
 Rm -rf - deletes directories

HOW GIT WORKS?

 Git init – Initialises a new git repository (with default branch as master)
 Git repository will be initially empty even if directory where you initialize git is not empty and
contains files and other directories
 Never manually change .git folder, let git manage itself

GIT OBJECT TYPES

 Blob – stores any files with extensions (.txt, .mp3, .mp4, etc)
o Have no file names
o Size and type is stored in each blob
 Tree – stores information about directories / folders
 Commit – storing different versions of the project
 Annotated tag – persistent text pointer to specific commits

GIT LOW LEVEL COMMANDS

 Git hash – object – able to create new object and write it in git repository
o Hash = folder name + file name
o For e.g. echo “HELLO” | Git hash-object –stdin -w
 Git cat -file – read information about git object
 Git mktree – make tree
JSON VS GIT

 Javascript Object Notation


o Format for data exchange between different servers (retrieve data from API from server to
web browser or send data to database to another server etc)
 Within JSON, keys should be unique, but it is possible to create the same keys but not
recommended
 However, keys in git is equal to hash of each object
o Key in git is generated based on value
o Git stores files based on their hash

HASH

 Takes any variable length input and creates a fixed length hash
 Hash is generated based on the contents of the file
 Hash are one-way functions (create based on input to create hash)
 Same input will generate same hash
 Hash types
o MD5 (128 bit)
o SHA1 (160 bits)
o SHA256 (256 bits)
o SHA384 (384 bits)
o SHA512 (512 bits)
 Git uses SHA1 hash function (which is hexadecimal format)

SHA1 HASH FUNCTION

 160 bits means (git can store 2^160 files in the same repository)
o Therefore the probability of a hash collision is 1/ (2^320)
 40 hexadecimal (base 16  0 – 9 and A - F ) characters
 Slight change to input data can lead to completely different hashes
 Commands such as shasum can be used in git bash to generate a hash
 Each time you save it creates a unique ID which is the SHA1 Hash

GIT CAT-FILE OPTIONS

 Git cat-file -p <hash (can enter first 4 characters atleast) > - contents of the object
 Git cat-file -s <hash> - size of the object (in bytes)
 Git cat-file -t <hash> - type of the object
 Git cat-file command doesn’t have options for retrieving the filename from the blob because blobs
don’t store filenames
GIT HASH-OBJECT

 Echo “hello, Git” | git hash-object --stdin -w (stdin  standard input and -w  create git object)
o Git hash-object <filename> -w
 Git repository stores files independently in its own file system in the objects directory
 Git generates SHA1 hash based on input+type+size

GIT OBJECT

 Do not store file names of the source file


 Contains contents, type, size and delimiters (\0)
 Name of each blob is based on a SHA1 hash of each file

TREES

 Representation of repositories
 Structure is the same of a blob (type, length and hash)
 Contents include permissions, type (tree), SHA1 hash and file name
PERMISSIONS

 040000 – directory (common)


 100644 – regular non-executable file (common)
 100664 – Regular non-executable group-writeable file
 100755 – Regular executable file
 120000 – Symbolic link
 160000 – Gitlink
 Find files
o Find .git/objects -type f

STEPS FOR MAKING TREE

 Nano temp-tree.txt
 <permission type> (space) <object type> (space) <sha1 hash of the object> (tab) <filename>

WORKING DIRECTORY, STAGING AREA AND GIT REPO

 Working directory – local directory


 Staging area (index) – responsible for repairing files to be inserted into git repository or prepare
files taken from git repository to be put into working directory
o Git ls-files -s : lists files in the staging area
o Git read-tree <hash> (gets files from git repo and puts in staging area)
o Git ls-files (lists files in git in staging areas)
o Git checkout-index -a (puts from staging area into working directory)
 Git repository – storage of files and folders using SHA1 hash

HOW MANY FOLDERS CAN GIT CREATE?

 16 (due to 16 different hexadecimal characters) * 16 (due to the folders having 2 characters) which
means 16^2 = 256 in git objects folder
COMMITS

 Commit contains same structure as blob and tree (object type, length and delimiter)
 Commit has information such as author name, email, commit description and parent
 Each commit has its own SHA1 hash (due to author name and email)
 Commits allow us to store in git database for different versions of the project
 Able to very easily and fast move to any version of the project by checking out specific commit
 Commit is a wrapper for the tree object and contains pointer to a specific tree

 By moving to different commits (checking out – taking files out of git repository and putting it into
your directory) you are able to “travel” between different “versions” of the project

SET GIT AUTHOR NAME AND EMAIL

 Git config - -global user.name <Name>


 Git config - -global user.email <Email>
 Git config - -list

CREATING COMMIT

 Git commit -m “HELLO”


o -m : Allows you to add a description
GIT BASIC COMMANDS

 Git status – current state of the git repository


 Git add – add files to staging area
o Git add . – stage all files
o Git add <filename> - stage specific file
 Git commit – write changes to git repository (creates new blob entries such as files in git repository)
 Git log – history of changes (commits)
 Git checkout – checkout commit or branch

GIT FILES LIFECYCLE

UNSTAGE FILES USING GIT RM

 Git rm –cached file3.txt


o Unstages files but does not remove them from the repository

PARENT COMMIT

 Check commit contents


o Git cat-file -p 44e073
 Parent is a pointer to the root commit (initial commit)

GIT CHECKOUT

 Git checkout – jump into certain version of the project


WHAT IS A BRANCH

 Branch is just a text reference to the commit (separate history of your project)
 Default branch is master
 Multiple branches can exist in the same repository
 Pointers for all branches are located in .git/refs/heads folder
 Current branch tracks new commits and creates new commit objects
o For example, you are in master branch and made several commits and checkout to a new-
branch and have made couple of commits. However, when you checkout to master branch,
you will not see commits from new-branch as the current branch tracks new commits and
those commits are not added to other branches but there are other ways to marriage
branches or rebase them.
 Branch pointer moves automatically after every new commit
 Change branch git checkout <branch>

WHAT IS HEAD IN GIT ?

 HEAD is reference to the currently checked-out branch or commit


 HEAD is locally significant
o When head points to specific commit instead of branch it is called detached HEAD
 Pointer is located in the cat .git/HEAD file
 Default pointer is ref: refs/heads/master
 Change reference to specific branch git checkout <branch>
 Change reference to specific commit git checkout <sha1>

GIT BRANCHES MANAGEMENT

 Git branch – list all local branches


 Git branch <name> - create new branch
o New branch will have contents from the current commit of the branch
 Git checkout <name> - checkout specific branch
 Git checkout -b <branch name> - shortcut for creating a branch with checkout
 Git branch -d <name> - delete specific branch (will delete only merged branch but if you want to
delete non-merged branch use “-D” option)
 Git branch -m <old> <new> - rename specific branch
GIT DIFF COMMAND

 Tracks different changes that are made between files.

You might also like