GIT & GITHUB (Basics & Commands)
GIT
Its a free and open source DVCS (Distributed Version Control System).
- Git is used to track changes in the source code
- The distributed version control tool is used for source code management
- It allows multiple developers to work together
- It supports non-linear development through its thousands of parallel branches
- Linus Torvalds created Git in 2005.
GIT is divided in 3 areas:
- Working directory: Gives space to code & modify files in your working directory
- Staging Area: Stage the files and add snapshots of them to your staging area.
- Local Repository (Git directory): Perform a commit that stores the snapshots permanently to your local Git directory. Checkout any existing version, make changes, stage them and commit.
Branch in GIT
- Branch is used to keep your changes until they are ready. You can do your work on a branch while the main branch (master) remains stable. After you are done with your work, you can merge it with the main office.
Commands used in GIT
- Create Repositories
git init - Make Changes
add
commit
status - Parallel Development
branch
merge
rebase - Sync Repositories
push
pull
add origin
GITHUB (Remote Repo)
Its a Central Repository where all the codes from local repo (final versions) is pushed and it is reflected to all connected users.
Its the remote repository which is connected to local git repo
We need to create an account at GIT Hub, Create Repository by providing Repository name by clicking like below by providing repository name:
The account can be Public (if need to use for free) as Private is paid.
For Authentication, password doesn’t work now so, you need to Generate New Token by following below steps:
- In the upper-right corner of any page, click your profile photo, then click Settings.
- In the left sidebar, click Developer settings.
- In the left sidebar, click Personal access tokens.
- Click Generate new token.
- Give your token a descriptive name.
- To give your token an expiration, select the Expiration drop-down menu, then click a default or use the calendar picker.
- Select the scopes, or permissions, you’d like to grant this token. To use your token to access repositories from the command line, select repo.
- Click Generate token.
Please save the generated code as same will be used at Git and it will be valid as the number of days mentioned by you in above steps.
Installation of GIT in Ubuntu or similar Kernels
Steps:
Get ZOhaib Ali’s stories in your inbox
Join Medium for free to get updates from this writer.
Run below command to update the packages in Ubuntu
$ sudo apt-get updates
Run below command to install the git package (should already be available)
$ sudo apt-get install git
Run below commands to check if package has been installed and which version it has:
(Note: Use double hiphens instead of single as not able to use here due to Medium restrictions)
$ which git
$ git -version
Now, configure username & and its email address:
$ git config -global user.name ‘userabc’
$git config -global user.name “userabc@gmail.com”
If you want to confirm that user details have been configured, run below:
$git config -list
Now, if you don’t have any account on github.com, please create one as it will used to link our local GIT so that our code can be pushed to this central repo for any updates or after our final code.
Now, we will create a directory where we will start GIT by initializing it and check its status:
# mkdir gitfolder
# cd gitfolder
# git init
#vi codefile1 (write your code in this file & save it by Esc > :wq)
# git status
On branch masterNo commits yet
Untracked files:
(use “git add <file>…” to include in what will be committed)
codfile1nothing added to commit but untracked files present (use “git add” to track)
Above means that that no ‘commits done yet’ on master branch and there is ‘untracked file’. So, if we want to push it to push it to staging area, we may use git add <file> for the same.
Now, we will add the file to staging area and again checking the status:
# git add .
#git status
On branch masterNo commits yet
Changes to be committed:
(use “git rm — cached <file>…” to unstage)
new file: file1
Now, moving the code to local repo (from staging area):
#git commit -m “<Any comments like My 1st commit for codefile1>”
You may again check the status (by # git status)
You may check the log (by # git log), where you can see commit id, who committed the changes, date/time and comments
# git log
commit 5e9ae3c299f8cde3a7d2acf8975f2cb1b58bbfdw (HEAD -> master, origin/master)
Author: userabc <userabc@gmail.com>
Date: Sun Jul 3 15:05:36 2022 +0000My first commit for file1
In above commit id is mentioned in alphanumeric ‘5e9ae3c299f8cde3a7d2acf8975f2cb1b58bbfde’
If we want to check the author, changes, version then it can be checked by
#git show 5e9ae3c299f8cde3a7d2acf8975f2cb1b58bbfdw
commit 5e9ae3c299f8cde3a7d2acf8975f2cb1b58bbfde (HEAD -> master, origin/master)
Author: userabc <userabc@gmail.com>
Date: Sun Jul 3 15:05:36 2022 +0000My first commit for file1
diff — git a/file1 b/file1
index e802362..86e8776 100644
— — a/file1
+++ b/file1
@@ -1,2 +1,4 @@
-Mon nom est Zohaib
-Comment tu t’appelle ?
+Comment allez vous
+?
+Ca va bien, Merci, beacoup!
Finally, add remote (your GitHub repo link) to push the code from Local repo to GIT HUB (central repository):
#git remote add origin https://github.com/userabc/centralrepo.git
#git push -u origin master
You will then be asked for GitHub Userid and Password (Generated Github token)
After successful push same can be checked in GIT Hub site where you may see the code and its versions. Old code can be checked in history.
How another user would work
Another user would also create a directory, configure user & email, initialize then will add repo link like below:
#git remote add origin https://github.com/userabc/centralrepo.git
Another user will now pull the code from GitHub:
#git pull origin master
Now userB can see the codefile1 which can be edited and saved. After this similar steps to add, commit and push will be followed and the same modified code will now be reflected at GitHub.









