Source Code Management and Version Control

SHYAM PANDEYSHYAM PANDEY
7 min read

setting up the tone

while working on group project large projects or any projects kyaa aapke man me aise kuchh kaam yaa sawal aaye hai

  • code me kuch change kiya fir realise huaa ye to galat hai aur fir se purane wale code pe jana chaha ho.

  • Jo bhi code aap likh rahe ho usko retain kar sako agar lost ho jaaye to ek backup rahe

  • kisi bhi large code base ki history jaan ni ho when and what changes huye hai isme

  • kisi aur ke code me contribute karna ho yaa group project me contribute karna ho.

  • koi new feature add kar ke test karna ho apne software me without interfering with working code.

agar aise sawal hai to uska ek sidha saa jawab hai use version control and source code management tools like git and github

what is version control tool?

version control tools are the tools which help in concurrent development,working together and maintaining history of the work to the software engineers.

what they help in

  • collaboration

  • maintaing versions

  • backups

  • restoring version

  • understanding what has happened to the code base over the time

Types

  1. Centralized version control tools

  2. Distributed Version control tools

ex:

distributed vcs :- git,mercurial

centraluzed vcs :- CVS,SVN

Distributed VS Centralized

CentralizedDistributed
There is one central server and enable team collaborationIt does not use central server that is why we can do multiple works offline like commit changes,create branch,view logs etc.
failure in the central server will stop all collaboration worksconnection is only needed to publish the changes or to get latest changes
works on two-tier architecture 1. working copy 2. repository
works on three-tier architecture 1. Working copy 2. Staging Area3. repository
checkout is used to get files from repository to our working copystaging area is the place where we organise all the files we are going to place in repository and can do staging using git add command
commit is used to putting files from working directory to repositorywe can commit changes from staging area to the repository using git commit

version control tool that we are more concerned about is git

we can easily download and configure git as it is open source product and free to use

git workflow

  1. modify the file in our working copy

  2. stage the files with changes ,adding snapshot of them in staging area

  3. commit the changes ,it will take the snapshots from staging area aoond publish them to repository

figure depicting workflow of git

Advantages of git

  1. Fast and small

  2. Free and open source

  3. Easier branching

  4. implicit backup

  5. security

Work cycle of git

important terminology

  1. Tree :- itas object , which represent a directory.

  2. commits :- they hold current state of the directory

  3. Branches :- They are required to create another line of developement for adding every new functionality a new branch can be created

  4. clone :- it creates an instance (blue print or copy ) of the repository

  5. pull :- pull option copies the changes from remote repository to local environment

  6. push :- it copies changes from local to remote

  7. head :- a pointer which always point to latest commits in the branch

  8. stash :- it is an operation used to put current work copy to back stage for a small period of time

Important git commands and works

  • Setting Up repository

    1. git init

      git init is used to create a new repository.it creates a new .git sub directory in the projects directory which contains all git meta data for that new repository.
      a --bare option is used with git init to create a bare repository(a repo which does not have working tree/directory).

    2. git clone
      git clone is a command which is used to create a copy of the repository at new directory, any new location
      git clone <repo> <directory> :- this command is used to clone a repo to specified directory

      shallow cloning is used to get specied version of a repository . agar koi aisaa repo hai jhaa bahut hi jyada commits ho rhe hai aur hame most recent commit yaa las 2 yaa last 3 (here last means most recent) commits tak hi repo clone karna hai to we use shallow cloning . it is done by

      git clone -depth=n <repo>:- where the value of n given to d will specify version which we want

  • Dealing with changes

    1. git add :- this command is used to add the chnages made in the local repo to the staging area

    2. git commit :- it a command used to commit the changes to remote repository. it commits whole snapshot from staged area not only the differences . it has multiple options with it, they are:
      git commit -a :- Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files.
      git commit -m :- allows to add commit message

      git commit -amend :- ammed the previous commited repository instead of creating a new one.

    3. git diff :- it is used to get differences in two repositories or two commits.

    4. git stash :- jab aap kisi ek feature pe kaam kar rhe ho but achanak se koi dikkat ke wajah se urgent koi aur branch ya code pe kaam karna pad jaaye to aap apne current work ko stash me store kar sakte hai .

      these are commands for changes

  • inspecting a repo

    1. git tag and git blame commands are used for inspecting
  • undoing changes

    1. git clean :- used to remove untracked files and repository
      we can use multiple options to specify paths and other things we can also make it interactive using git clean -di

    2. git revert :- it creates a new commit which undoes all the changes made in previous (only one) commit .(it actually takes repo one commit back )

    3. git reset:- it takes back to a previous commit discarding changes mmade after that commit

    4. git rebase <base> :- rebase the current branch to <base>.<base> can be commit id , branch, tag or a relative reference to head.

other important commands

  1. git remote :- it help us to create a view and delete connection with other repositories. it also list the remote connections you have with other repositories

    git remote -v will aslo show urls of all connections you have

  2. git remote add <name> <url> :-Create a new connection to a remote repository. After adding a remote, you’ll be able to use <name> as a convenient shortcut for <url> in other Git commands. example :- (i). git remote add origin <url>

    (ii). git push origin master , here instead of providing url of repo we have given the name origin in push command

  3. git fetch :- git fetch command downloads commits,files from a remote repo to your local repo.

    to fetch only specified branch we use, git fetch <remote> <branch>

  4. git push :- add the local repo to remote .

  5. git pull :- it downloads the files from re,ote repo and merge it with local repo. it first runs fetch command and then merge command locally.

  6. git branch :- used to create branch . ex:- git brach feature , it will create a new branch called feature.

  7. git checkout :- used to navigate through branches . ex:- git checkout feature , it will take you to brach called feature.

  8. git merge:- used to merge a branch to another branch . use git status command to make sure that the head is pointing to the position where you want to merge.if head is not at right position we must checkout to right branch before merging.

  9. git log :- this is used to log version history.

  10. git status :- this shows all modified files which are not committed

    This conclude the basic knowledge about git and version control

    THANK YOU FOR READING

0
Subscribe to my newsletter

Read articles from SHYAM PANDEY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SHYAM PANDEY
SHYAM PANDEY