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

GIT Notes

Uploaded by

Shyam Duvvapu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

GIT Notes

Uploaded by

Shyam Duvvapu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Overview of GIT

what is Version control system ? why Version control system ?


Traditional way of storing the code.
Types of Version control system(VCS)
Architecture of Git
Installation of Git
Basic Commands
- git init
- git --version
- git add
- git status
- git commit
- git config
- git log
- git show
===================================================================================
==
Branches
===================================================================================
===
Tags
Merge & Rebase
===================================================================================
==
Revert
cherry-pick
===================================================================================
======
Github Account Creation
===================================================================================
======
Advanced commands
- git pull
- git push
- git clone
- git remote
Github Administration
- GUI -> Branch, tags, commit files
- Branch & Tag protection rules
- SSH and HTTPS Clone
- Collabrators
- General Settings
PullRequest
Fork mechansim
==================================================================================
GIT - Global Interaction Tool / Global Tracking Tool/System.

Git is Distributed version control system/source code management which provides


proper tracking and benefit of storage optimization for storing the
code/scripts/file/directories etc

==============================================================================
What is Version control system ?
==============================================================================
Version control system provides an unique version for tracking and optimizing the
storage cost via SHA 125/256 with help of commit id's.
==============================================================================
Why Version Control system ?
==============================================================================
1. Tracking : username - name
email - email-id

Each and every person who modifies the file - space/change


word/change line/add/delete - everything is tracked.
VCS - will track who modified/added/deleted - which
line/word/sentence
Proper Timestamp - date, time, seconds etc

2. Storage cost:

Feature : login page

DAY 1 : login.java -> 1 GB data -> execution & testing -> stable/working file
Record the changes via commit id -> dnon93b9f2bibnad02nr20osdf
commit-id -> unique random id -> SHA-125/256 (secure hashing alogirthm) -
16 digit octect - alpha-numberic

DAY 2: login.java -> 4 GB data -> execution & testing -> stable/working file
Record the changes via commit id -> mxjdhdjh983bfb9bb9bbfd
commit-id -> unique random id -> SHA-125/256 (secure hashing alogirthm) -
16 digit octect - alpha-numberic

DAY 3 :login.java -> 10 GB data -> execution & testing -> stable/working file
Record the changes via commit id -> lsdmdjho0h0h9ddud00d
commit-id -> unique random id -> SHA-125/256 (secure hashing alogirthm) -
16 digit octect - alpha-numberic

=============================================
Traditional way of storing the code:
=============================================

Windows/Linux :

1. Tracking : last modified person name


last modified time & date
size - change in size

problem: Tracking the people name - who modified


- what time & date

which line/word/statement - delete/add/modify/append

2. Storage cost :

Feature : login page

DAY 1 : login.java -> 1 GB data -> execution & testing -> stable file -> 10 lines
- Day -2 - 3 lines

backup_login.java_27062023 - 1 GB data

DAY-2 : login.java -> 1 GB + 3 GB = 4 GB -> execution & testing -> stable file -
20 lines
backup_login.java_28062023 - 4 GB data

DAY -3: login.java -> 4 GB + 10 GB -> -> execution & testing -> stable file

===================================================================================
=====================
Types of Version control system(VCS):
===================================================================================
=====================
1. Centrailized VCS - clearcase, SVN
2. Distributed VCS - Git/Github , bitbucket
===================================================================================
================================
Architecture of GIT :
===================================================================================
================================
GIT - CLI
GITHUB - GUI

Working directory
Staging Area
Local Repo
Remote Repo
===================================================================================
================================
Installation of Git :
===================================================================================
=============================
Method 1 : Package Manager

yum install git -y

Method 2 : Download and Install via tar bundle. Refer Page 6

===================================================================================
==============================
Basic commands:
===================================================================================
==============================
1. How to check git version
command: git --version

[root@ip-172-31-17-229 ~]# git --version


git version 2.39.3
[root@ip-172-31-17-229 ~]#

===================================================================================
============================
2. How to create a repository in git ?
command: git init Repository_name
(OR)
mkdir - /opt/project
cd project
git init

Repository is nothing but an folder/directory with some additional mechanism of git


for performing git operations.
[root@ip-172-31-17-229 git-operations]# git init
Initialized empty Git repository in /opt/git-operations/.git/
[root@ip-172-31-17-229 git-operations]#
===================================================================================
============================
3. How to update the git configuration?

command: git config --list


git config user.name "GoudSagar"
git config user.email "[email protected]"

[root@ip-172-31-17-229 git-operations]# git config --list


core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=GoudSagar
[email protected]
[root@ip-172-31-17-229 git-operations]#
===================================================================================
===========================
vi file1.txt
add contents -> save file

[root@ip-172-31-17-229 git-operations]# git status


On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt

nothing added to commit but untracked files present (use "git add" to track)
[root@ip-172-31-17-229 git-operations]#
===================================================================================
===========================

4. How to add file to git control for tracking the file?

command : git add file_name


git add file1 file2
git add .

[root@ip-172-31-17-229 git-operations]# git add .


[root@ip-172-31-17-229 git-operations]#
[root@ip-172-31-17-229 git-operations]# git status
On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file.txt

[root@ip-172-31-17-229 git-operations]#
===================================================================================
===========================
5. How to check where is my file located currently?

command: git status

===================================================================================
===========================
6. How to record the file changes?

command: git commit -m "Ticket - Messages"

[root@ip-172-31-17-229 git-operations]# git commit -m "Adding new file"


[master (root-commit) c267b4f] Adding new file
1 file changed, 1 insertion(+)
create mode 100644 file.txt
[root@ip-172-31-17-229 git-operations]#

===================================================================================
===========================
7. How to check the commits created ?

command: git log // list all the commits with detailed info
git log -n1 // last commit
git log -n2 // last 2 commits
git log --oneline // short commit and messages

[root@ip-172-31-17-229 git-operations]# git log


commit c267b4fde5e37e23354d7b1f8f1c2d8b3a4c5eb4 (HEAD -> master)
Author: GoudSagar <[email protected]>
Date: Fri Jun 30 04:21:15 2023 +0000

Adding new file


[root@ip-172-31-17-229 git-operations]#

[root@ip-172-31-17-229 git-operations]# git log --oneline


c267b4f (HEAD -> master) Adding new file
[root@ip-172-31-17-229 git-operations]#

===================================================================================
==============================
8. How to check detailed information on the commit ?

command: git show commit_id

[root@ip-172-31-17-229 git-operations]# git show


c267b4fde5e37e23354d7b1f8f1c2d8b3a4c5eb4
commit c267b4fde5e37e23354d7b1f8f1c2d8b3a4c5eb4 (HEAD -> master)
Author: GoudSagar <[email protected]>
Date: Fri Jun 30 04:21:15 2023 +0000

Adding new file

diff --git a/file.txt b/file.txt


new file mode 100644
index 0000000..78513c1
--- /dev/null
+++ b/file.txt
@@ -0,0 +1 @@
+THis is first git operation
[root@ip-172-31-17-229 git-operations]#

===================================================================================
================================

Note :
(use "git rm --cached <file>..." to unstage

===================================================================================
================================
Branches :
===================================================================================
==============================
Branches are nothing but logical folders/directories who stores all the
files,directories and unqiue commits.
Branch segregates the work keeping the files/directories and commits uniquely.
With branches, we can re-use the code for other applications.4
Branches are very cheap thats means we can create and delete a branch easly and
branches doesnt occupy more spaces.

eg : TREE(GIT)
|
BRANCH(short) BRANCH(tall)
BRANCH(big)
|
Sub branch(folders) flowers(files) fruits(commits) flowers fruits
Sub branch fruits

eg : Ecommerce website
* login page
* logout oage
* product catalog
* payment pages
* add to cart

Single Branch :
Developer -> Branch(master) -> login.java - 1000 lines -> 15 commits
logout.java - 500 lines -> 10 commits
product catalog - 2000 lines - 30 commits
payment pages - 1000 lines -> 20 commits
add to cart - 200 lines - 10 commit

Feature wise branches : login-feature branch -> login.java - 1000 lines -> 15
commits
login-security.java

logout-feature branch -> logout.java - 1000 lines -> 15


commits
logout-security.java

product-feature branch ->product.java - 2000 lines -> 15


commits
product-security.java

Final Branch - ecommerce-website branch or (master) -> combine all the above
branches into single -> build and generate aritfact.

===================================================================================
=
Standard Branches :
===================================================================================
=
1. master/main branch : Contains always latest and updated code/scripts
2. develop branch : Development and testing code/script
3. release branch : Ready to move for production/live
- flipkart-release-1.0
- flipkart-release-2.0
4. feature branch: Feature releated branches
* login-page feature branch
* payment-page branch
5. Hotfix branch : Live issues or customer tickets are managed here
* logout-issues-ticket-number

apart from this , we can create branches with any names


===================================================================================
=
Commands:
===================================================================================
=
git branch // list all branches
git branch branch_name //create the branch
git checkout branch_name // switch a branch same as linux cd command
git checkout -b branch_name // create & switch branches
git branch -D branch_name // forcefully delete a branch
git branch -d branch_name // delete a branch
===================================================================================
=
Tags: Tag is checkpoint or bookmark for marking a commit as stable point of
release.

eg : v1.0, R1.0, Release-stable-1, flk-0.1 etc

command: git tag -l // list all the tags


git tag tag_name // create a tag and assign/mark the
tag to latest commit_id
git tag tag_name commit_id // create a tag and assign/mark the
tag to particular commit _id
git tag -f tag_name commit_id // existing tag will re-assigned to
particular commit
git tag -d tag_name // delete a tag
===================================================================================
===========================
Merge & Rebase :
===================================================================================
===========================
Merge : Merge is used for combining changes(commits/files/directories) from one
branch to another branch.
======================
Types of Merge:
======================
1. Fast forward merge :

eg : master M1 M2 M3 M4
master > git merge develop
develop M1 M2 M3 M4 M5

[root@ip-172-31-17-229 git-operations]# git merge develop


Updating 8bcf6d6..c6f6e9a
Fast-forward
f33 | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 f33
[root@ip-172-31-17-229 git-operations]#

2. Non Fast forward merge :

eg : master M1 M2 M3 M4 M5 M6 M7 MC(merge commit)

master > git merge develop

develop M1 M2 M3 M4 M5 M6

master M1 M2 M3 M4 M5 M6 M7 MC

master > git merge develop

develop M1 M2 M3 M4 M5
M6=================================================================================
==================================
Rebase : Rebase is used for re-writing the history on top of base branch. Rebase
will keep the history clean without combining any commits together.

eg : develop M1 M2 M3 M4 M5 M6 M7 M8

develop > git rebase release

release M1 M2 M3 M4 M5

eg : develop M1 M2 M3 M4 M5 M6 M7 M8

develop > git rebase release

release M1 M2 M3 M4 M5 M6 M8

===================================================================================
=========================================
Revert : Revert is mechanism used to undo/rollback the changes and revert will
create new commit for every rollback/undo operation perform.

command : git revert commit_id


===================================================================================
=========================================
cherry-pick : cherrypick is process of picking up one commit from one branch and
putting the commit in another branch.

command: master > git cherry-pick commit_id

eg : master M1 M2 M3 M4 M5 M8

master > git cherry-pick M8

develop M1 M2 M3 M4 M5 M6 M7 M8
===================================================================================
=====================================
Github Account Creation:
===================================================================================
==================================
Github Administration : PART-1
===================================================================================
==============================
1. How to create repository?

2. How to create branch ?

3. How to tag a commit ?

4. How to create/update file and record the changes via commit id ?

5. How to check commitId?

6. How to check details info about commitID?


===================================================================================
========================
Advanced commands:
===================================================================================
==============
git clone: Cloning is process of downloading the repository.

===================================================================================
=======
Types of clones :
===================================================================================
=======
1. HTTPS : URL - https://github.com/GoudSagar/training-devops-work.git

command : git clone https://github.com/GoudSagar/training-devops-work.git

[root@ip-172-31-17-229 http-clone]# git clone


https://github.com/GoudSagar/training-devops-work.git
Cloning into 'training-devops-work'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 2), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), 28.64 KiB | 2.60 MiB/s, done.
Resolving deltas: 100% (2/2), done.
[root@ip-172-31-17-229 http-clone]#
Authentication : UserName :GoudSagar
Password : <token>

How to Generate Token for https clone/push/pull?

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

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 ---shyam
===================================================================================
===
2. SSH : URL - [email protected]:GoudSagar/training-devops-work.git

command: git clone [email protected]:GoudSagar/training-devops-work.git

[root@ip-172-31-17-229 ssh-clone]# git clone [email protected]:GoudSagar/training-


devops-work.git
Cloning into 'training-devops-work'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 2), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), 28.64 KiB | 14.32 MiB/s, done.
Resolving deltas: 100% (2/2), done.
[root@ip-172-31-17-229 ssh-clone]#

How to generate and uplaod ssh public key to Github ?

a. generate ssh key : ssh-keygen -t rsa

b. go to profile -> settings -> SSH & GPG Keys -> add new ssh key

===================================================================================
=====================
Ticket : project.txt -> 10 lines
commit your changes in local repo

vi project.txt
git status
git add .
git status
git commit -m "Ticket - messages"
===================================================================================
=====================
git push : Push is mechanism for uploaded the changes(commits/files/directories)
from local repo to remote repository.

command: git push origin main

origin is short/alias name for github url.


verify: git remote -v

===================================================================================
===================================
git pull : Pull is mechanism used for downloading the updated changes from remote
repository to local repository.
command: git pull origin main

[root@ip-172-31-17-229 training-devops-work]# git pull origin main


remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 466 bytes | 466.00 KiB/s, done.
From github.com:GoudSagar/training-devops-work
* branch main -> FETCH_HEAD
4fc5b22..00a8046 main -> origin/main
Updating 4fc5b22..00a8046
Fast-forward
project.txt | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 project.txt
[root@ip-172-31-17-229 training-devops-work]#

============================================================================
git pull = git fetch + git merge

Method 1: git fetch origin main


- fetch the changes to origin/main
- git log --oneline main -> matching with local branch and remote
branch
- git log --oneline origin/main
main> git merge origin/main

Method 2: git fetch origin main


git checkout -b new-feature origin/main
===================================================================================
git fetch: fetch is process to download the updated changes to intermediatory
branch called origin/branch_name.

command: git fetch origin main

[root@ip-172-31-17-229 training-devops-work]# git fetch origin main


remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 669 bytes | 669.00 KiB/s, done.
From github.com:GoudSagar/training-devops-work
* branch main -> FETCH_HEAD
00a8046..465b8de main -> origin/main
[root@ip-172-31-17-229 training-devops-work]#

[root@ip-172-31-17-229 training-devops-work]# git merge origin/main


Updating 00a8046..465b8de
Fast-forward
fetch-mechanism.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 fetch-mechanism.txt
[root@ip-172-31-17-229 training-devops-work]#
===================================================================================
============================
Remotes: Git Remote is central clearing house where we are storing all the
code/scripts etc

Alais -> Short name for github url's for easily identifying the repo url and easily
pushing & pulling changes to & from
remote repos.

Commands : git remote -v -> Display all the remote repos


git remote add upstream [email protected]:goudsagar/Ansible-repo.git
git remote remove upstream [email protected]:goudsagar/Ansible-repo.git
git remote update origin [email protected]:johnsmith/kubernetes-repo.git

git remote -v
origin [email protected]:bathinisreelatha/Ansible-repo.git(push)
origin [email protected]:bathinisreelatha/Ansible-repo.git(fetch)
upstream [email protected]:goudsagar/Ansible-repo.git(push)
upstream [email protected]:goudsagar/Ansible-repo.git(fetch)

syntax: git remote add <alais> < url >


command: git remote add upstream [email protected]:goudsagar/Ansible-repo.git

git push origin employee


git pull upstream company
================================================
Fork mechansim :
A fork is a copy of a repository. Forking a repository allows you to freely
experiment with changes without affecting the original project.
===================================================================================
===============================
Pull Request - Creating pull request, review mechanism
=======================================================================
Pull requests help you collaborate on code with other people. As pull requests are
created, they’ll appear here in a searchable and filterable list.
===================================================================================
============================
Github Adminsitration:
===================================================================================
===========================
1. Github Account Settings
2. GITHUB Activities : branch/tag/repository/create file/commit from GUI
3. Repository Settings :
* Collabrators : Permission
* Branch protection from deletion
* Restriction the Merge into company account - Minimum 2 review and always pull
request to be raised
4. Tag protection from deletion
5. SSH Keys upload
6. HTTPS Token Generation
7 . Webhooks - Covered in Jenkins
8. Fork Mechanism
===================================================================================
====================

You might also like