Git and Github Basics
Git and Github Basics
Introduction:
Git requirements:
os independent, track everything, unique id (using sha-1), track history, no content change
Git objects:
Blob (file and its metadate: file type, permissions, size)
Tree (dirs. and its metadata)
commit
tagged annotation
Each commit has a unique id (sha-1 hexadecimal 40 char length)
Installation on local
From the GitHub:
create a new public repo – don’t mark create readme – so you can get the below help cmds -,save the
link from code
to delete a repo, go to settings and then Danger Zone and click on delete repo
generate token (it will be used instead of password to authenticate from local env to github)
It will ask you to put your github username and password (deprecated)
now we can only supply a token (that we generate from the github gui) – enter the token instead of
password
Creating a new file
Adding/ Removing from staging area (index tree) – git add <file> / git rm –cached <file>
Note how git stores info about the file once tracked in both the staging and local repo
Not the type of the file 100 and permissions 644 also commit status 0
Note the object name and object dir name in the repo tree
This is how to check the type, size and content of a given git object
To add the file to the local repo, use add commit – not the objects in the staging and the local repo..
Now 3 objects are there… the file object, the tree and the commit that links the object and tree
together. This is the snapshot
If we modified anything in file.txt – if we checked the status – you will find it modified
Note the parent line in this commit file – the parent line is a pointer to the pervious commit so that the
git can track the changes – each commit is pointing to its previous commit. The commit that has no
parent is called the root commit.
The branch is a chronological sequence of commits. The default branch is called Master Branch
The current working dir is corresponding to the last commit – if we point the head to a previous commit
The content of the working dir (working tree) will correspond to that previous commit …
If we changed again the file by adding a new line – Note the use of command git diff to show the
difference of the file
git diff is showing the difference between the working dir and the staging area (index)
Note what happened if we add an empty line at end
After adding the changes to staging area - git diff is not showing any changes
To get the difference between the staging and the repo – use the git diff with flag –staged—
You can also filter the commits congaing the blob ids of a given object (or commits done by a certain
developer…
You can also check a graphical ref for all git commands. :
https://marklodato.github.io/visual-git-guide/index-en.html
Please note removing a file will also need git add and git commit
Also if you want to rename a file (pls use git mv – not mv)
Undoing things
To delete repo – just delete the .git
You can also change the last commit text using git commit –amend
Rollback / Roll forward to a certain commit
You can rollback by resetting the head to a certain commit – you can
rollback to the staging area (preferred) – so you can check the differences
(using git diff) – or you can rollback to work dir using the --hard flag
This file with 4 line – each line with diff commit – as you can see 4 commits – you can see commit with
git log and you can see commit details with git show and head is now on the c4 in branch master
Head is just a file contains a pointer to certain commit
To go backward one revision ( notice the flag –hard to apply the rollback to
work dir)