What is git?

Git basics -

What is Git?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

Previously ---

1.  Linux was used bit-keeper after that Linux stated the git

2.  Git stores a snapshot of the entire projects. If some files have no changes, git does not store them again.

Creator                       Linus Torvalds (Linux creator)
Developer(s)               Junio Hamano and others[
Initial release             7 April 2005; 14 years ago 0.99
Stable release             2.22.0 / 7 June 2019; 12 days ago
Written in                   C, Shell, Perl, Tcl, Python
Operating system        POSIX: Linux, Windows, macOS
Available in                 English
Type                           Version control
Website                      git-scm.com

What is VCS (Version Control System)?

1.  A way to keep track of changes in files.
2.  Collaborations between developers.
3.  Keep track of all WHO did what.
4.  Merge and Revert.
5.  Easy recovery if something is messed up. if system crased.
6.  Provides historical records.

Before git other previous version control systems are

1.  Subversion
2.  Perforce
3.  Mercurial
4.  Clearcase
5.  CVS

Problem in previous VCS

VCS systems used Centralised repository means if internet connection is not present then we can not commit the changes. Or we can loose our changes. This was the main problem in VCS.

Centralized version control system (CVCS) uses a central server to store all files and enables team collaboration. It works on a single repository to which users can directly access a central server.

Please refer to the diagram below to get a better idea of CVCS :

 

Operations -

   Branch  Commit  Merge Revert

For solving this problem, Git introduce new system DVSC.

What is Distributed Version Control System?

1.  Git started new system like one copy of our repository is in our server and one copy is in our local machine.
2.  We can push our code any time if our code in completed.
3.  Everyone has their own local copy of code and can work simultaneously on their own branches. Git works when you’re offline since almost every operation is local. 


In Distributed VCS, every contributor has a local copy or “clone” of the main repository i.e. everyone maintains a local repository of their own which contains all the files and metadata present in the main repository.  
 

You will understand it better by referring to the diagram below:

   

 Introduce new concept
    a) Local Repository
    b) Local Branch
    c) Local Commits

New operations of git

    a) pull
    b) push

Versions of Git or Web hosting services?

   1.  Git is open source. Git is only a command line interface but the git versions are provides graphic interface.
   2.  Git is CLI, Web hosting services provides web based GUI
   3.  Git web hosting services
        GitHub,
        GitLab,
        Savannah,
        BitBucket and
        SourceForge all offer online code hosting.
   4.  These all are fully based on Git. Provides graphic interface or web based interface. We can check all the things easily.
   5.  Most famous git implementations-
        Public and private profile.

What is git repository?

1.  A repository hosted on Internet is called Remote Repository.

2.  It's a folder where all the data of our project will be stored.

Difference between normal folders and git repositories?

Normal Folder  - Normal folders only contains files and directories.

Git Repository  - Git Repository contains files, directories along with their complete history.

Git commands -

1)  git init

     For initializing the git repository in our local system. Created .git folder (hidden)

2)  git config --global

     --global flag

      1.  Multiple repositories can be created in single machine.
      2.  If you use the --global flag, the setting will be applied to all the repositories of the machine.
      3.  If you want this identity only for this repository then do not use --global flag.

      git config --global user.name "Rohit"
      git config --global user.email "rohityadav905@gmail.com"
      git config --global core.editor gedit" 

      If we want to see the colors in git command then use

      git config --global color.ui true

     Check identity config list

      git config --list

3)  git add

       git add

4)  git status

       1.  Staging area -> Need to commit the changed files.

       git status

5)  git commit

       1.  We can save our work into repository.
       2.  Each commit is saved with the information of username, email id , date, time and commit message.

       git commit -a -m "commit message"

6)  git log

       1.  It's showing commit SHA id
          commit  s874s5s3bsdf54d4d2d24et2df2gdfgf73983ba
          Author: Rohit Yadav <rohityadav905@gmail.com>
          Date:   Thu Jan 20 13:16:27 2019 +0530

      SHA-1 hash

        1    It is a unique id of 40 alpha-numeric characters.
        2    Git stores all the information in its database by the hash value.
        3    Git commits are identified by the SHA-1 hash.

       git log

       Wants to see commit message and commit hash in single line

       git log --oneline

7)  git commit -a -m

     -a and -m flag

      -a flag  ->  add all the files into staging area.
      -m flag ->  give commit message in command line.

      we can use both as -am

      git commit -am "commit message" 

8)  If we want to remove files from local commit. then we can use

      git rm --cached mypage.html

9)  If we want to add remove file in previous commit then

      git log

      commit  s874s5s3bsdf54d4d2d24et2df2gdfgf73983ba
      Author: Rohit Yadav <rohityadav905@gmail.com>
      Date:   Thu Jan 20 13:16:27 2019 +0530

      git checkout s874s5s3bsdf54d4d2d24et2df2gdfgf73983ba mypage.html

      git status

     we can see the mypage.html file 

     new file:  mypage.html

10)  git checkout .

        Remove all local changes.

        Switch to particular commit  ->  git checkout commit_hash_id

12)  git diff OR git diff filename

        git diff mypage.html

        git diff [source branch] [target branch]    

        Preview changes before merging

13)  Check difference between any two commits

       commit 485sfsd5fsdfsd1sdf5ds5dfsfdfsf
       Author: Rohit Yadav <rohityuvasoft149@gmail.com>
       Date:   Thu Jan 20 13:16:27 2019 +0530

                 Task - New file

       commit 848sfsd4fsdf8sddf4d4e863e5d9e3
       Author: Rohit Yadav <rohityuvasoft149@gmail.com>
       Date:   Thu Jan 20 12:09:50 2019 +0530

                 Task - New changes in the api

       git diff 485sfsd5fsdfsd1sdf5ds5dfsfdfsf  848sfsd4fsdf8sddf4d4e863e5d9e3

14)  Check difference between last two commits

      git diff HEAD HEAD~

      It's showing difference between last two commits (485sfsd5 and 848sfsd4).
       HEAD      -->   The latest revision is always HEAD.
       HEAD~    -->   The latest -1 revision is always HEAD~ or HEAD~1
       HEAD~2  -->   The latest -2 revision is always HEAD~2
       HEAD~3  -->   The latest -3 revision is always HEAD~3

15)  git show OR git show commit_id

       It shows latest commit information

16)  If we want to see all commits of particular page

       git blame mypage.html

17)  git help <verb>

        git help show
        git help commit

        <verb> means any git command name. 

18)  git branch

       Work with the new module without disturbing main project. The default branch is master.

      Create a new branch

        git branch branch_name

      Create a new branch and switch to it

        git branch -b branch_name

       I will checkout all the current branch files and switch it into master branch.

        git checkout --force master

19)  Merge code into branches

        Merge master branch code in new_branch

        git checkout new_branch
        git merge master

        Revert merge code from new_branch

        git reset --head HEAD~

20)  Merge code into branches

        Merge master branch code in new_branch

        git checkout new_branch
        git merge master

21)  Delete Branch

        git branch -d branch_name

22)  git stash

      1.   Save stash with stash message

            git stash save "stash message"

      2.   See stash list

            git stash list

              stash@{0}: WIP on development: 548sf54 saved
              stash@{1}: WIP on development: 548dsb4 saved

      3.   If we have 5 stash. We want to see any particular stash info

            git diff stash@{0}

      4.   git stash apply (apply command only apply the stash but it will not delete the stash)

            git stash apply

      5.   Apply particular stash

            git stash apply stash@{1}

      6.   Delete particular stash

            git stash drop stash@{1}

      7.   Apply first stash and delete that stash

            git stash pop

      6.   Delete all stash

            git stash clear

Also see :-

Comments

Popular posts from this blog

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

SEARCHING USING AJAX IN RAILS

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

UPLOAD ANY MEDIA FILE IN RAILS USING PAPERCLIP

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.