Git Notes
Git Notes
DevOps
Study
Material
INDEX
Topic-1: Introduction to Devops
Topic-6: Example-1 To Understand Working Directory, Staging Area and Local Repository
Topic-15: Git Aliases - Providing our own convenient names to git commands
Topic-6: Example-1 To Understand Working Directory, Staging Area and Local Repository
Topic-15: Git Aliases - Providing our own convenient names to git commands
Topic-16: Ignoring unwanted files and directories by using .gitignore file
Topic-17: Any Special Treatment for directories by Git ???
Topic - 1
Introduction
to DevOps
Topic - 1: Introduction
to DevOps
1.1) What is Devops?
1.2) Water Fall Model
1.3) Agile Model
1.4) Water fall vs Scrum
1.5) Devops vs Agile Models
1.6) Top Important points about DevOps
For any software development,release and maintenance, there are two groups of
engineers will work in the company.
1) Development Group
2) Non-Development Group or Operations Group or Administrators Group.
1)Development Group:
The people who are involving
1) planning
2) coding
3) build
4) Testing
2)Operations Group:
The people who are involving
1) Release
2) Deploy
3) Operate
4) Monitor
Eg:
Release Engineers
Configuration Engineer
System Admin
Database Admin
Network Admin
etc
1) Waterfall Model
2) Prototype Model
3) Incremental/Iterative Model
4) Spiral Model
5) RAD Model
6) Big-Bang Model
7) Fish Model
8) V Model
9) Agile Model
Requirement
Gathering
Requirement
Analysis
Design
Coding
Testing
Release
Maintenance
Advantages:
1) It is very simple and easy to implement.
2) Phases won't be overlapped and hence there is no ambiguity.
3) All phases will be executed one by one which gives high visibility to the project
managers and clients about the progress of the project.
4) Best suitable if the requirements are fixed.
5) Best suitable for small projects.
Disadvantages:
1) It is very rigid model b'z it won't accept requirement changes in the middle.
2) Client satisfaction is very low because most of the times client will add new
requirements in the middle,which won't be supported.
3) Total project development time is more because testing should be done after
complementing development only.
4) The cost of bug fixing is very high because we cannot identify bugs in the early stages
of life cycle.
5) Not suitable if the requirements keep on changing.
6) Not suitable for large projects.
Among all these models Scrum model is the most popular and frequently used model.
Scrum is derived from Rugby Game.
It is light weight process.
It is an iterative /incremental model and it accepts changes very easily.
It is people based model but not plan based model.
Team Collaboration and Continuous feedback are strengths of this model.
Points to Remember:
1) Scrum is an agile model that allows us focus on delivering highest quality software in
shortest time.
2) In this model software developement follows increment by increment
3) Each increment will take one to 3 weeks duration.
4) 7 to 9 members are responsible in every sprint.
The art of doing the twice work in half time is nothing but scrum model Juff sutherland
Limitations:
1) The chances of project failure is very high if individuals are not committed or
cooperative
2) Adapting scrum model for large teams is very big challenge
3) Must required experienced and efficient team members
4) If any team member leaves in the middle of project,it can have a huge negative impact
on the project.
Similarities:
1) Both are software development methodologies. Agile is there in the market for the
last 20 years, but devops is recent methodology.
2) Both models concentrating on rapid development of software project.
Differences:
1) The differences between these models will starts after development of the project.
Agile methodology always talks about software development,testing and deployment.
Once deployment completed agile methodology has no role.
But Devops model will continue after deployment also and it is also responsible for
operations and monitoring.
2) In Agile Model, separate people are responsible for developing, testing, and deploying
the software. But, in DevOps, the DevOps engineer is responsible for everything;
development to operations, and operations to development.
4) Agile model always giving highest priority for speed, where as Devops giving proirity
for both speed and automation.
5) In Agile, client is responsible to give the feedback for the sprint. But in Devops,
immediate feedback is available from the monitoring tools.
What is Devops?
Devops is not a new Tool/Technology in the market.
client project
|--100 files developed
|- client suggested some changes
|- I changed some files source code to meet client requirement
|- I gave the demo and client suggested some more changes
|- I changed some files source code to meet client requirement
|- I gave demo 3rd time
|- Client asked for first version only
|- My Face with big ????
Repository:
Where we have to store files and metadata.
Here version control is applicable.
Here we can talk about versions like version-1, version-2 etc
Commit:
The process of sending files from working directory to the repository.
Checkout:
The process of sending files from repository to working directory.
2) All commit and checkout operations should be performed by connecting to the central
repositoty via network. If network outage, then no version control to the developer.
i.e in this type, developer work space and remote repository server should be
connected always.
3) All commit and checkout operations should be performed by connecting to the central
repositoty via network and hence these operations will become slow, which causes
performance issues. No local operations and every version control operation should be
remote operation.
1) The checkout and commit operations will be performed locally. Hence performance is
more.
2) To perform checkout and commit operations network is not required. Hence if there is
any network outage, still version control is applicable.
3) If something goes wrong to any repository there is a chance to recover. There is no
question of single point of failure.
4) To perform push and pull operations network must be required, but these operations
are not most common operations and we are performing very rarely.
Note:
1) commit and checkout operations will be performed between workspace and
repository.
work space – commit Repository
Repository – checkout workspace
The main job of remote repository is just to share our work to peer developers.
High availability, Speed and there is no single point of failure are main reasons for
popularity of this model.
1) Distributed
Git is developed based on Distributed Version Control System Architecture.
Because of Distributed Architecture it has several advantages:
A) Every Developer has his own local repository. All the operations can be performed
locally.Hence local repo and remote repo need not be connected always.
B) All operations will be performed locally, and hence peformance is high when compared
with other VCSs. i.e it is very speed
C) Most of operations are local. Hence we can work offline most of the times.
D) There is no single point failure as Every Developer has his own local repository.
We cannot commit the files of working directory directly. First we have to add to the
staging area and then we have to commit.
This staging area is helpful to double check/cross-check our changes before commit.
This type of layer is not available in other Version Control Systsem Tools like CVS, SVN etc
Git stores files in repository in some hash form, which saves space.
GIT will uses internally snapshot mechanism for this. All these conversions and taking
snapshots of our data will be happened in staging area before commit.
Eg: If a sample repository takes around 12 GB space in SVN where as in GIT it takes hardly
420 MB.
We can merge multiple branches into a single branch. We can commit branch wise also.
4. Moving files in GIT is very easy as GIT automatically tracks the moves. Whereas in other
VCS we need to create a new file & then delete the old one.
For every developer, a separate local repository is available. Developer can perform all
checkout and commit operations wrt local repository only.
To perform commit operation, first he has to add files to staging area by using git add
command, and then he has to commit those changes to the local repository by using git
commit command. Hence commit in GIT is a 2-step process.
commit is applicable only for staging area files but not for working directory files.
If the developer wants to share his work to the peer developers then he has to push his
local repository to the remote repository by using git push command.
Remote repository contains total project code, which can be accessible by all developers.
New developer can get local repository by cloning remote repository.For this we have to
use git clone command.
A developer can get updates from the remote repository to the local repository by using
git pull command.
git add To add files from working directory to staging area.
git commit To commit changes from staging area to local repository.
git push To move files from local repository to remote repository.
git clone To create a new local repository from the remote repository.
git pull To get updated files from remote repository to local repository.
Topic – 4
Life Cycle
of
File in GIT
Topic - 4: Life Cycle of
File in GIT
Every file in GIT is in one of the following states:
1)Untracked:
The files which are newly created in working directory and git does not aware of these
files are said to be in untracked state.
2)Staged:
✽ The files which are added to staging area are said to be in staged state.
✽ These files are ready for commit.
4)Modified:
Any file which is already tracked by git, but it is modified in working directory is said to
be in Modified State.
Topic - 5
Git Installation
On
Windows
Topic-5: Git Installation On
Windows
https://git-scm.com/download/win
2.26.2
Git-2.26.2-64-bit.exe
If we just type git, then we will get complete options available with git command.
lenovo@DESKTOP-ECE8V3R MINGW64 ~
$ git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
diff Show changes between commits, commit and working tree, etc
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.
Topic – 6
Example-1 To Understand
Working Directory,
Staging Area and
Local Repository
Topic-6: Example-1 To Understand Working
Directory, Staging Area and Local Repository
1) Creating workspace
2) git initialization
3) Creating files with some content in the working directory
4) Adding these files to staging area
5) Git Configurations before first commit
6) Commit those changes to local repository
lenovo@DESKTOP-ECE8V3R MINGW64 ~
$ cd d:
lenovo@DESKTOP-ECE8V3R MINGW64 /d
$ mkdir gitprojects
lenovo@DESKTOP-ECE8V3R MINGW64 /d
$ cd gitprojects
lenovo@DESKTOP-ECE8V3R MINGW64 /d/gitprojects
$ mkdir project1
lenovo@DESKTOP-ECE8V3R MINGW64 /d/gitprojects
$ cd project1
lenovo@DESKTOP-ECE8V3R MINGW64 /d/gitprojects/project1
Now project1 acts as working directory. We have to request git, to provide version control
for this directory. For this we have to use git init command.
git init This command will provide empty repository for our working directory, so that
version control is applicable for our workspace.
The name of the empty directory is .git, which is hidden directory.
No commits yet
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.txt
b.txt
nothing added to commit but untracked files present (use "git add" to track)
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: a.txt
new file: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
But make sure this option will work only for modified files, but not for newly created files.
1)git init
Once we creates workspace, if we want version control, then we require a local
repository. To create that local repository we have to use git init command.
$ git init
Initialized empty Git repository in D:/gitprojects/project1/.git/
2)git status:
It shows the current status of all files in each area, like which files are untracked, which
are modified, which are staged etc.
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: a.txt
modified: b.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git status -s
M a.txt
M b.txt
A c.txt
3)git add:
To add files from working directory to staging area for tracking/commiting purpose, we
have to use git add command.
4)git commit:
If we want to commit staged changes, then we have to use git commit command.
For every commit, a unique commit id will be generated. It is of 40-length hexadecimal
string.
$ echo -n "df4bb05e36e672698251e05e09d92ba45ea1fc47" | wc -c
40
The first 7 characters also unique, by using that also we can identify commit.
We can add files to staging area and we can commit changes by using a single command
But this command will work only for tracked files but not for new files.
5)git log:
It shows history of all commits.
It provides commit id, author name,maild , timestamp and commit message.
6)git config:
We can use this command to configure git like user name, mail id etc
git config --global user.email "[email protected]"
git config --global user.name "Durga"
***Note:
global means these configurations are applicable for all repositories created by git. If we
are not using global then it is applicable only for current repository.
We can change user name and mail id with the same commands
7)$git ls-files
This command will listout all files which are tracked by git.
8)$ls
This command will listout all files present in workspace
$ git log
commit 48437a7ad2ada6e18a26b127ca101c0ebf45b19e (HEAD -> master)
Author: Durga <[email protected]>
Date: Thu May 7 21:09:33 2020 +0530
commit 3a8051f59110f9696f4e0f922f438cbb6bb7694d
Author: Durga <[email protected]>
Date: Thu May 7 21:06:58 2020 +0530
commit 4b77312160c82d76395558da415a96b2a8b36072
Author: Durga <[email protected]>
Date: Thu May 7 21:05:25 2020 +0530
commit 93d297b69e048046b8ff5dba140b5889f1b47500
Author: Durga <[email protected]>
Date: Thu May 7 20:59:22 2020 +0530
commit d49f79120beecb2ea9e34b8398b4ee78bf662bf4
Author: Durga <[email protected]>
Date: Thu May 7 20:52:12 2020 +0530
commit d49f79120beecb2ea9e34b8398b4ee78bf662bf4
Author: Durga <[email protected]>
Date: Thu May 7 20:52:12 2020 +0530
Note: There are multiple options are availble for git log command to see the history.
git log --help
Output:
7 characters of commit id + commit message
***This option is very helpful if we have lot of commits and to identify commit based on
message.
Option-2: -n Option to Limit the Number of commits
to Display
We can limit the number of commits in the git log command output.
For this we have to use -n option.
Syntax:
-<number>
-n <number>
--max-count=<number>
Limit the number of commits to output.
$ git log -n 2
commit b7bd0cfecb7cd64128f209a1de4cc0ffefdd9310 (HEAD -> master)
Author: Ravi <[email protected]>
Date: Sat May 16 21:23:23 2020 +0530
commit 44fe2785f2e3f30ebcf733ffdc278ce240364488
Author: Durga <[email protected]>
Date: Sat May 16 21:05:07 2020 +0530
It shows all commits which has given pattern in the commit message.
*** This option is very helpful if we follow a particular structure for the commit message.
We can use this option to find all commits related to a particular request number or
defect number etc.
commited a.txt
commit b7bd0cfecb7cd64128f209a1de4cc0ffefdd9310
Author: Ravi <[email protected]>
Date: Sat May 16 21:23:23 2020 +0530
Note: There are multiple options are availble for git log command to see the history of all
commits.
Demo Example:
file1.txt
First line in file1.txt
Second line in file1.txt
file2.txt
First line in file2.txt
Second line in file2.txt
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
file2.txt
First line in file2.txt
Second line in file2.txt
Third line in file2.txt
Fourth line in file2.txt
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
file1.txt
First line in file1.txt
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
sixth line in file1.txt
3)--- a/file1.txt
--- means missing lines in staged copy
4) +++ b/file1.txt
+++ means new lines added in working directory version
5) @@ -3,3 +3,4 @@
-3,3
- means source version
@@ -3,3 +3,4 @@
Second line in file1.txt
Third line in file1.txt
Fourth line in file1.txt
Fifth line in file1.txt
+sixth line in file1.txt
Clear indication that one line added in the working directory copy when compared with
staged copy.
Eg:
$ git diff e5705a6 file1.txt
diff --git a/file1.txt b/file1.txt
index d4effe0..e3e329f 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,2 +1,6 @@
First line in file1.txt
Second line in file1.txt
+Third line in file1.txt
+Fourth line in file1.txt
+Fifth line in file1.txt
+sixth line in file1.txt
Summary:
git diff <path>
Shows the differences in the content of working directory, staging area and local
repostiory.
We can use P4Merge tool for both comparison and merging purposes.
https://www.perforce.com/
downloads
skip registration
P4MERGE will provide multiple utilities, But we require only Merge and Diff Tool.
Select only Merge and Diff Tool.
$ p4merge
bash: p4merge: command not found
Mergetool Configurations:
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:\Program Files\Perforce\p4merge.exe"
git config --global mergetool.prompt false
Note: p4merge tool can be used to compare only one file at a time.
Topic - 11
Removing Files
by
using git rm Command
Topic-11: Removing Files by
using git rm Command
It is very common requirement to remove files from working directory and staging area.
For these removals we can use the following commands
git rm file1.txt
git rm --cached file1.txt
General Linux rm command
git rm file1.txt
file1.txt will be removed from staging area and from working directory
$ git rm .
fatal: not removing '.' recursively without -r
It won't work because we didn't use -r option.
$ git rm -r .
It will remove all files
$ ls
file1.txt file2.txt
$ rm file1.txt
Note:
1) git rm file1.txt It will remove file from both working directory and staging area
2) git rm --cached file1.txt It will remove file only from staging area but not from
working directory
3) rm file1.txt It will remove file only from working directory but not from staging area.
Topic - 12
Undo Changes
with
git Checkout Command
Topic-12: Undo Changes with
git Checkout Command
We can use checkout command to discard unstanged changes in the tracked files of
working directory.
It is something like undo operation. It will copy contents of the file from index
area(staging area) to working directory.
Eg:
$ git checkout -- file1.txt
It will discard any unstaged changes made in file1.txt.
After executing this command, staged copy content and working directory content is
same.
$ cat file1.txt
first line in file1.txt
second line in file1.txt
This is third line in file1.txt
This is fourth line in file1.txt
Note: git checkout is applicable only for the files which are already tracked by git. It is not
applicable for new files.
Summary:
git checkout -- file.txt
To discard changes in working directory copy.
git checkout
To discard changes in all tracked files of working directory.
git checkout
If we are not passing any argument, then this command will show the list of eligible files
for checkout.
Git provides some sample names for these commit ids. We can use these names directly.
These are just pointers to commit ids. These sample names are called references or refs.
Eg:
$pwd
/d/gitprojects/project6/.git/refs/heads
$ cat master
49aa8d79a9bab4c0d72dec217c0c6d5d96d604ce
Most of the times, we have to use the most recent commit id.
For such type of most commonly used commit ids git provides default references.
What is master?
$ git status
On branch master
If any reference pointing to another reference, such type of reference is called symbolic
reference. Hence HEAD is symbolic reference.
$ cat HEAD
ref: refs/heads/master
Detached HEAD:
Sometimes HEAD is not pointing to the branch name, such type of head is considered as
Detached HEAD.
Topic - 14
Git reset
Command
Topic-14: Git reset
Command
git reset command is just like reset settings in our mobile.
Changes already added to staging area, but if we don't want to commit, then to remove
such type of changes from staging area, then we should go for git reset.
It will bring the changes from staging area back to working directory.
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file1.txt
nothing added to commit but untracked files present (use "git add" to track)
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file1.txt
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file1.txt
nothing added to commit but untracked files present (use "git add" to track)
git rm --cached vs git reset:
git rm --cached file1.txt
The file will be removed completely from staging area.
git reset file1.txt
The file won't be removed from staging area, but reset to previous state(one step back).
Q) We modified the content of the file1.txt and added to staging area. But we
want to ignore those changes in staging area and in working directory. For
this requirement which commands we required to use?
Syntax:
git reset <mode> <commitid>
Moves the HEAD to the specified commit, and all remaining recent commits will be
removed.
mode will decide whether these changes are going t0 remove from staging area and
working directory or not.
Example:
vi file1.txt
First line in file1.txt
vi file2.txt
First line in file2.txt
vi file3.txt
First line in file3.txt
To discard commit-3:
git reset --mixed 86d0ca3
git reset --mixed HEAD~1
git reset HEAD~1
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
file2.txt
file3.txt
nothing added to commit but untracked files present (use "git add" to track)
Note:
1) It is not possible to remove random commits.
2) --mixed will work only on repository and staging area but not on working directory.
3) whenever we are using --mixed, we can revert the changes, because changes are
available in working directory.
The commits will be discarded only in local repository, but changes will be there in
working directory and staging area
$ git ls-files
file1.txt
file2.txt
file3.txt
Use Cases:
1) If some files are missing in the last commit, then add those files and commit again.
2) We forgot to add defect number in commit message.
2. --soft
Changes will be discarded only in local repository.
It won't touch staging area and working directory.
Working tree won't be clean.
But we can revert with
git commit
3. --hard
Changes will be discarded everywhere.
Working tree won't be clean.
No way to revert.
Note:
If the commits are confirmed to local repository and to discard those commits we can use
reset command.
But if the commits are confirmed to remote repository then not recommended to use
reset command and we have to use revert command.
Topic - 15
Git Aliases –
Providing our own Convenient Names
to
git Commands
Topic-15: Git Aliases - Providing our own
Convenient Names to git Commands
Alias means nickname or short name or other alternative name.
In Git we can create our own commands by using aliasing concept. This is something like
alias command in Linux.
If any git command is lengthy and repeatedly required, then for that command we can
give our own convenient alias name and we can use that alias name every time.
Note: After creating alias name, we can use either alias name or original name.
Q2) Create alias Name 's' to the following git Command?
git status
$ git s
git: 's' is not a git command. See 'git --help'.
$ git config --global alias.s "status"
$ git s
On branch master
nothing to commit, working tree clean
Note: If we use git in original command while creating alias name, what will happend?
$ git config --global alias.ss "git status"
.gitconfig:
[user]
name = Ravi
email = [email protected]
[core]
autocrlf = true
[diff]
tool = p4merge
[difftool "p4merge"]
path = C:\\Program Files\\Perforce\\p4merge.exe
[difftool]
prompt = false
[merge]
tool = p4merge
[mergetool "p4merge"]
path = C:\\Program Files\\Perforce\\p4merge.exe
[mergetool]
prompt = false
[alias]
one = log --oneline
s = status
We can perform any changes in alias commands based on requirement.
one = log
$ git one
commit bb26af3c6875a480ee0f92883ba85af5048eec6f (HEAD -> master)
Author: Ravi <[email protected]>
Date: Tue May 26 19:40:13 2020 +0530
commit 257073dcecf4364b77e8c64dbd7386a71f4071a2
Author: Ravi <[email protected]>
Date: Tue May 26 12:38:38 2020 +0530
file1 added
Topic – 16
Ignoring unwanted Files
And
Directories by using .gitignore File
Topic-16: Ignoring unwanted Files and
Directories by using .gitignore File
It is very common requirement that we are not required to store everything in the
repository. We have to store only source code files like .java files etc.
.gitignore File:
We have to create this file in working directory.
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
Customer.java
a.txt
b.txt
logs/
nothing added to commit but untracked files present (use "git add" to track)
.gitignore:
# Don't track a.txt
a.txt
#Don't track all .txt files
*.txt
#Don't track log files
logs/
#Don't track any hidden file
.*
Topic – 17
Any Special Treatment
For
Directories by Git?
Topic-17: Any Special Treatment for
Directories by Git?
No special treatement for directories.
Git always consider only files but not directories.
Git never give any importance for the directories.
Whenever we are adding files from the directory, implicitly directory also will be added.
$ git status
On branch master
nothing to commit, working tree clean
$ mkdir dir1
Even though we created dir1, GIT won't give any importance for this directory because it
does not contain any files.
$ git status
On branch master
nothing to commit, working tree clean
$ touch dir1/{a..d}.py
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
dir1/
nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: dir1/a.py
new file: dir1/b.py
new file: dir1/c.py
new file: dir1/d.py