GIT Notes
GIT Notes
Def
s
Git is a platform independent i.e.., it will open in all systems
Op
It is a free and open Source
Git save time and developers can fetch and create pull requests without switching
It is 3rd generation of VCS
ev
VCS History
tD
1. SCM(Source code control management) - To track only one file
Gi
2. RCS(Revision control system) - Track multiple files but not directories
3. CVS(concurrent version system) - Track multiple files and directories but single user
a
al
4. SVN(sub version) - Track multiple files & directories and multiple users
ikk
Stages in GIT
Ch
p
ee
nd
Sa
Working Directory :
Untracked files are present here. When a new file is created, updated or an existing file deleted, those
changes automatically go into the working area.
Staging Area:
Here, changed files are present here. So, we can commit/save
Repository:
Types Of Repo:
Local Repo:
s
The local repository is everything in your git directory. Mainly what you will in your local repository are
Op
all of your checkpoints (or) commits. It is the area that saves everything (so don’t delete it)
Remote Repo:
ev
The remote repository is a git repository that is stored on some remote computer. It allows you to
tD
centralize the work done by each developer
Central Repo:
s
We have to initialize the empty repository. Otherwise, file will not track/commit
Op
git init . ( . represents current directory)
ev
GIT ADD (Track the files)
tD
Git add command is straight forward command. It add files to the staging area
We can add single or multiple files at once in the staging area
Gi
Every time we add or update any file in our project, it is required to forward updates to the staging
area
a
The staging and committing are co-related to each other
al
git rm --cached *
GIT STATUS
The git status command is used to display the state of the repository and staging area
It allows us to see the tracked, untracked files and changes
This command will not show any commit records or information
GIT COMMIT
It is used to record the changes in the repository
It is the next command after the git add
Every commit contains the index data and the commit message
git commit -m “enter the commit message” . ( . represents every changed/ tracking files)
s
git commit -a -m “enter the commit message” filename
Op
ev
See the list of commits history in git
git log
tD
git log --oneline
Gi
If you want see the commit file name
a
al
Note:- We can’t commit the committed file. We can commit if that file has any changes
ee
GIT Configure
nd
If you want to give your username and E-mail id to those commits then
Sa
--global :- If we give global means that particular commit name or email id applies to all the git
repository. If we don’t give global also it works but it will work on that particular/current repository
Note: now giving the git log command to see changes, it won’t work because after configuring we
haven’t done anything. Now create a file and commit that file and give git log you will see changes as
you configure.
GIT Ignore
It will be useful when you don’t want to track some specific files then we use a file called .gitignore
create some text files and creates a directories with “jpg” files
vi .gitignore
*.txt
Now all the text files will be ignore
If you really again want that file use: git add -f *
Note: If you want to ignore a file, before tracking only you have to be put. Otherwise, it will be no use
s
Op
Changing the details for Latest commit
ev
After performing this command, one file opened, don’t do anything. perform ‘wq’ .
tD
--amend : It’s a flag, if you want to edit the commit details use this command
Deleting commits
nd
Reset
1. --mixed : This is the default option(remove commit and move changes to working area)
2. --soft : If we give soft means files not deleted only commit is deleted
3. --hard : It removes commits and permanently discarded changes without our permission
Note:- If we perform above command we don’t have any commits but inside files data is deleted
Revert
s
Revert used to delete specific/particular commit
Op
git revert commitID
ev
reset vs revert
Reset removes commit from the history and revert will not
tD
Reset works for local commits and revert works for local and revert commits
Branches Gi
A branch represents an independent line of development
a
The git branch command lets you create, list, rename, and delete branches
al
Note:- If you perform git branch first time, you can’t see any branch including master also. You can see
Sa
when you commit something first time to repo. Otherwise, master is not visible
The -d option will delete the branch only if it has already been pushed and merged with the remote
branch.
Use -D instead if you want to force the branch to be deleted, even if it hasn’t been pushed or merged yet.
The branch is now deleted locally
s
Note:- If you create one file inside a branch from master. It will be present in all branches & master. But,
Op
whenever you are committing the file into a particular branch. It will not present
Feature Branch
ev
They are short lived branches.
tD
We can simply called ephemeral i.e. short lived objects
we delete it after it is integrated to develop
Merging
Gi
Getting the all commits from one branch to another branch. means getting files and commits from one
a
branch to other
al
Note:- First, which branch you need to merge, go inside that branch and perform that merging
ikk
Eg: so, I’m going to master i.e.., git checkout master then you can do merge
Ch
GIT rebase
If you have 5 commits in master branch and only 1 commit in devops branch, to get all the commits from
nd
cherry-pick
Cherry pick picks specific/particular commits from another branch and merges with the current
branch
We can pick through commitID
Eg:- If you have 5 commits in master branch and only 1 commit in devops branch, to get specific commit
from master branch to devops branch. we can use cherry pick
git cherry-pick commitID
merge conflicts
cat > file : hi all ⟶ add & commit ⟶ git checkout -b branch1
s
Op
cat > file : 0987654 ⟶ add & commit ⟶ git checkout master
ev
Now perform , git merge branch1
tD
We have to do manually to resolve our conflicts
stash
ikk
Using the git stash command, developers can temporarily save changes made in the working
Ch
directory.
It allows them to quickly switch contexts when they are not quite ready to commit changes
And it allows them to more easily switch between branches
p
It saves changes in working and index areas and saves it to a different location and making a way
ee
Remove particular stash ⟶ git stash drop stashID eg:(git stash drop stash@{0})
merge vs rebase
When there are changes on the main branch that you want to incorporate into your branch, you can
either merge the changes in or rebase your branch from a different point
Merge takes the changes from one branch and merges them into another branch in one merge
commit
Rebase adjusts the point at which a branch actually branched off (i.e., moves the branch to a new
starting point from the base branch)
Generally you’ll use rebase when there are changes are made in main/master branch that you want
to include in your branch.
You’ll use merge when there are changes in a branch that you want to put back into main
to merge : git merge branch_name
to rebase: git rebase branch_name
s
If I delete/modify the branch in local, I want to see the changes in server ⟶ git fetch -p
Op
ev
github
tD
It is a web-based platform used for version control
It simplifies the process of working with other people and makes it easy to collaborate on projects
Gi
Team members can work on files and easily merge their changes in with the master branch of the
project
a
In github, default branch is ‘main’ branch
al
Readme file: It is a text file, it tells about the particular repository what is the version and what
actually the code does, how to use/see the repo, all information developers are write inside a readme
Ch
Blame: This command presents the developer details at each and every line
Commands
p
ee
Linking the repo from local to central ⟶ git remote add origin url
push the code from local to central ⟶ git push -u origin branch_name
nd
Delete github branch from local ⟶ git push -u origin --delete github_branch_name
From local to central we have to send our code use git pull
git pull origin branch_name
git pull
git pull = git fetch + git merge
git fetch origin branch_name
git merge origin/branch_name
fetch : Inside the central whether we have the changes/not. we will know through by git fetch
(or)
s
It will download remote commits to local but it will not merge
Op
(or)
ev
You can review remote changes before you merge
tD
Pull Request:
Git Cloning:
ikk
Git Fork:
nd
A fork is a rough copy of a repository. Forking a repository allows you to freely test and debug with
changes without affecting the original project
Sa
(or)
Speed
Simplicity
Fully Distributed
Excellent support for parallel development, support for hundreds of parallel branches
Integrity
DisAdvantages:
s
has long history
Op
Comparison
ev
tD
Gi
a
al
ikk
Ch
p
branching strategy
ee
s
to that release. And we merge this release back to main
Op
So, every time you have release you must create release branch like release 1, 2, 3, ......
hotfix ⟶ we use this branch for fixing production defects. And this is created from release and make
sure this fix is included into the next release. that should be taken by DevOps people
ev
eg: we are having defects in release 1 and we fix those defects using this hotfix branch
tD
This is one of the common flows used in real-time
Trunk-Based Strategy Gi
Trunk means main branch
a
we will not have so many branches in this approach
al
It won’t help in bigger teams. It is helpful only for less team i.e. 1 or 2 developers
Ch
iii. release-0.1.0
Patch: There is a bug, if you want to fix it that is called patch release Eg: tier puncture patches etc..,
Git Hooks/Scripts
We can execute custom scripts (or) specific operations
Advantage:
When you go with this approach, we know very sure what is the commit, which task the commit is
made
If you go to JIRA It should all commits made particular tasks
There are server side and client side hooks. Git supports both
When you perform operations on server side, server side hooks comes into the picture
s
Op
ev
tD
Gi
a
al
ikk
Ch
p
ee
nd
Sa