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)
click on profile photo and select setting
From local:
Doing things
inorder that git keeps track of who made changes,supply your username and email
git config --global user.name "hebawork"
git config --global user.email refayheba152@gmail.com
git commit --amend --reset-author
git init -------> creates hidden .git dir
git add README.md -------> add to staging area
git commit -m “first commit” -------> each add must be followed by commit
git branch -M main -------> we will use the main branch
git remote add origin https://github.com/hebawork/myproject.git -------> points to remote Repo
git push -u origin main -------> to push to main branch (only first time we push we write origin main)
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
Initializing a git repo
What is .git
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
Add file to staging and check status
Commit the change to local repo
Check the log – notice that we have now 2 commit objects
Check the objects in the git repo – notice the new commit obj starting CF
Every commit on a single file creates 3 objects
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—
To config an editor to enter your commit comments – use below command
So just do git commit and the vim editor will be opened
To check the commit comment – use git log
To see the log in short format
You can also filter the commits congaing the blob ids of a given object (or commits done by a certain
developer…
Show only last 2 commits
To inspect a certain commit id – using one command (use git show <commit-id>
To get the difference between the 1st and last commit
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
To remove files from staging
Use git rm –cached <file > to remove file from staging area
To restore the files from the repo use the git restore <file name>
git restore is restoring the file from the staging area
To unstage a file : use git restore –staged
Then use git restore to undo the change (restore from repo)
You can add to staging and commit in one step
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
You can see diff between commits
To go backward one revision ( notice the flag –hard to apply the rollback to
work dir)
Notice that git log now shows 3 commits
Use git reflog to see all commits and the resets
To roll forward (fast forward)