Brief Introduction To GIT ( DVCS)

Bakul Gupta
6 min readSep 26, 2019

--

GIT

  1. LCS (Local Control Systems ) A database was maintained, responsible for keeping track of various local changes to a specific directory or a files Example:- RCS
  2. CVS ( Centralised Version Control System ) Allows developers or multiple people to collaborate on the same project
  • Includes various features such as versioning of the software ,log messages generation

Example:-

1. Subversion
2. Perforce

Disadvantages:

Single Point Of Failure :

Corruption of hard-disk leads to havoc :

No hierarchical model is possible i.e multiple collaboration at a time

  1. DVCS ( Distributed Version Control System )

Each contributor has its own copy of the project. In case if the server goes down, and server data is corrupted. Then any of the contributors that upload the code to the repository. No single point of failure Flexibility to push local changes to the remote repository

Example :-

  • Git
  • Mercurial
  • Bazaar
  • Darcs

History of GIT

Year Milestone

2002 Linux kernel uses proprietary DVCS called BitKeeper

2005 Relationship between BitKeeper and Linux kernel maintainer broke down

2005 Lead towards the development of open-source dvcs (git)

GIT Storing scheme:

Streams of snapshot

Other:

Store changes as list of file-based changes

Also known as delta version control

Git has integrity: Checksummed every piece of information with SHA-1 algorithm (40 character string)

Three states of GIT

  1. Committed: Means data is successfully stored in the local database
  2. Modified: Means u have changed the file but not yet committed
  3. Staged :

Three-section of Git project:-

  1. Git Directory: Stores the metadata and object database
  2. Working Tree :
  3. Staging Area

| echo “Hello” > first.txt; echo “World” > second.txt | — → working Tree | git add first.txt | — → staging Tree | git commit -m “Ready to be committed” | — → git Directory

Installation on ubuntu

sudo apt-get install -y git-all

Check the git version

git --version

Location of git-config file

locate gitconfig
find . -name .gitconfig -type f
cat ~/.gitconfig | cat ~/.git/config | cat /etc/gitconfig

Initial Git Setup

Step 1:- Setup your name

git config --global user.name "Bakul Gupta"

Step 2:- Setup your email-id

git config --global user.email "bakulgupta11@gmail.com"

Step 3:- Setup the default editor for git

git config --global core.editor vim

Step 4:- See the git configuration settings

git config --list

Step 5:- Also show the location of the git config file

git config --show-origin --list

GIT BASICS

Initialize a git repository

cd /home/bakul/Git
git init

Add files to the staging area

echo "Hello World" > first.txt
git add first.txt

Commit the file

git commit -m "First file added"

Clone an existing repository

git clone <url of the repository>Example :- git clone https"//github.com/BullHacks3/try.git

Clone a git repo into a particular directory

git clone <url of git repo> <name_of_destination_directory>

Stage of a file

Untracked -> Unmodified -> Modified -> Staged

Check the status of the files

git status [file_name]

Attribute of git status command

git status -s
or
git status --short

Ignoring the files

echo " *.cpp " >> .gitignore
It means ignore all the files with extensions .cpp to be added in the git repository

Example of git ignore file includes

# Ignore all files ending with .secret
*.secret
# Do track bakul.secret ,even u r ignoring the *.secret files
!bakul.secret
#Ignore all files in the directory hacking
hacking/
#Ignore all the files in the doc/direcotry an any of its subdirectories
doc/**/*.pdf
For more example refer to the following link :- https://github.com/github/gitignore

To show the differences between the various files in git

git diff
git diff --staged
git diff --cached

Remove a file or directory from a git repository

git rm first.txt
git rm /directory

Rename a file in git

git mv first.txt second.txt
or
mv first.txt second.txt
git rm first.txt
git add second.txt

See the logs of the repository

git log                   : Show the commit log ,in reverse chronological order
git log -p (--patch) : Show the logs along with the differences
git log --stat : Show the stat of the log
git log --pretty=oneline : Provide the one line output for each commit
git log --pretty=short
git log --pretty=full
git log --pretty=fuller
git log --pretty=format: "%h %s" --graph
git log --since=1.weeks
git log --grep
git log -S function_name

Undoing the changes

If u make an early commit ,and forgot to add the file ,then u can make use of ammend option
git commit --amend

Un-staging a staged file

git reset HEAD [fiile_name]
Example:-
git add first.txt
git reset HEAD first.txt

Un-modifying a modified file

git checkout -- [file_name]

Working with Remotes

To see the remote URLs

git remote
git remote -v

To add a remote to a repository

git remote add origin [git_repo url]

To update the repository

git fetch <remote>

Git fetch vs git pull

git pull : git fetch + git merge
git fetch : download all the changes to local system but doesn't merge the changes automatically

Pushing your remotes

git push <remote> <branch>
Example: git push origin master

Inspecting a remote

git remote show origin

Renaming and removing remotes

git remote rename origin newremote
git remote rm newremote

Tagging

Tagging allows to keep track of versioning of the projects
git tag : used to list the tag available in the git repository
git tag -l "v1.0" : used to search the repo with particular version

Creation of tags

Annonated Tags
git tag -a v1.0 -m "First version 1.0"
LightWeight Tags
git tag v1.1-lw

Pushing the tag

git push origin <tagname>
or
git push origin --tags

Git Aliases

git config --global alias.c checkout
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD --'

Git Branching

Finds its application when one needs to perform multiple features related tasks on the git repository. Or When multiple developers need to work on the same project Creates a new branch with name testing

git branch testing

Switch to a different branch

git checkout testing

Creation as well as switching to a different branch

git checkout -b testing

Git Merging Techniques

  1. Fast Forward technique
  2. Recursive merge technique

Delete a remote branch

git push origin --delete [remote_branch_name]

Git Rebasing

In git, there are two ways to integrate the changes from one branch to another branch

  1. rebase
  2. merge

Rebase: it used to deploy the changes on branch to another branch Used to make commit looks like in serial order instead of parallel execution

git checkout testing
git rebase master
Example:
git rebase --onto master server client
It means consider the client branch,and apply those patches that are diverged from the server branch
git rebase master server
It will rebase a server branch on top of master branch

Git Insights

Various protocols used by the git.Usually, git make use of four protocols

  1. Local
  2. HTTP 2.1 Smart HTTP Protocol 2.2 Dumb HTTP Protocol
  3. Secure Shell
  4. Git 4.1 Dedicated port(9418)

Types Of Workflow in Git

  1. Centralized workflow
  2. Integration-Manager Workflow
  3. Dictator and Lieutenants Workflow

Git Tools

Contains information about the HEAD and branch references ```git reflog git log -g

To view the logs in more descriptive way

git log — pretty=format: ‘%h %s’ — graph

Show the changes that have not been merged into the master branch

git log testing..master

### Stashing the work
git stash command is used to temporarily save work on current working directory

git stash apply git stash — list git stash drop

Enjoy Learning @@@

--

--

Bakul Gupta

Security Engineer | Trying to learn something new each and everyday!!!