Total Pageviews

100,761

Sunday, 29 January 2017

Git for Source Code Management !!!

For source code management, different organizations make use of different tools. In my previous post we discussed about one such SCM tool which is SVN. In this post, I am going to discuss about Git. Now a days Git is the most popular and commonly used SCM tool.

Unlike in SVN, Git makes use of two repos.

1. Local repo
                    This is local to developers workspace. His/her locally stored workspace.

2. Remote repo
                    This is actual repo maintained by Git server. Every time a developer works and commits to his local repo, if he/she is satisfied with the changes then he will push these changes to remote repo.

Why Organizations prefer Git than SVN:
            Main reason is, Git is a Distributed Version Controlling System and can be maintained both on premises and on cloud.

Distributed because: Every developer has their local repos distributed, but the main commits can be done/shared to/from the remote repo (Git server).

ON CLOUD:

               There are different online open source providers for hosting Git repo.

github, gitbasket and gitlab                         --- on cloud
stash, gitblit and gerrit                                  --- on premises
collaborator and gitblit                                  --- both

Open Github.com,



Click on Sign in and enter required details:



Choose according to wish, since for practice I am selecting public repository.


Fill in the details:


Click on New repository:


Give a name for your repo, If we include a README file, repo will be created along with it.


If you want a Private repo, then do.....



Find the Https URL:


Now all we need to do, is to install Gitblit client in our local machine. 

https://git-scm.com/download/win                         File: Git-2.11.0.3-64-bit.exe

It is a normal .exe, installation process is next--next--next...
Use command prompt or git bash (Git bash is preferable for Linux users)

       user@USER-PC MINGW64 ~$ pwd
              /c/Users/user
       user@USER-PC MINGW64 ~$ git --version
              git version 2.11.0.windows.3

Go to desired and use git clone to checkout our repo:

       user@USER-PC MINGW64 ~$ cd F:/
       user@USER-PC MINGW64 /f $ cd Workspace_local/
       user@USER-PC MINGW64 /f/Workspace_local $ ls
              antivir.txt  hp_sr.txt  subversion/
       user@USER-PC MINGW64 /f/Workspace_local $ git clone https://github.com/oncloudrepo/git_repo.git
              Cloning into 'git_repo'...
              remote: Counting objects: 3, done.
              remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
              Unpacking objects: 100% (3/3), done.
       user@USER-PC MINGW64 /f/Workspace_local $ ls
              antivir.txt  git_repo/  hp_sr.txt  subversion/

       user@USER-PC MINGW64 /f/Workspace_local $ cd git_repo/
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ ls
              README.md
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $

Copied a file to my workspace..

       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ ls
              antivir.txt  README.md


       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ git status
              On branch master
              Your branch is up-to-date with 'origin/master'.
              Untracked files:
                (use "git add <file>..." to include in what will be committed)
                     antivir.txt
              nothing added to commit but untracked files present (use "git add" to track)
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $

A file which is created, but yet to be added ==== UnTracked file

       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ git add .
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ git status
              On branch master
              Your branch is up-to-date with 'origin/master'.
              Changes to be committed:
                (use "git reset HEAD <file>..." to unstage)
                       new file:   antivir.txt
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $

Before committing, only README file is in repo....



A file which is added, but yet to be committed ===== Staged File

       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ git commit -m "anti file add"
              [master 90ba8e9] anti file add
               Committer: USER <USER >
              Your name and email address were configured automatically based
              on your username and hostname. Please check that they are accurate.
              You can suppress this message by setting them explicitly. Run the
              following command and follow the instructions in your editor to edit
              your configuration file:
                         git config --global --edit
              After doing this, you may fix the identity used for this commit with:
                         git commit --amend --reset-author
                         1 file changed, 11 insertions(+)
                         create mode 100644 antivir.txt
              user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $

A file which is committed to local repo, but yet to be pushed ===== Tracked File

       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ git status
              On branch master
              Your branch is ahead of 'origin/master' by 1 commit.
              (use "git push" to publish your local commits)
              nothing to commit, working tree clean
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $

So far file was committed to our local repo, now we need to push to remote repo to reflect these changes in Git server.

       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master) $ git push




              Counting objects: 3, done.
              Delta compression using up to 4 threads.
              Compressing objects: 100% (3/3), done.
              Writing objects: 100% (3/3), 399 bytes | 0 bytes/s, done.
              Total 3 (delta 0), reused 0 (delta 0)
              To https://github.com/oncloudrepo/git_repo.git
               1785bc4..90ba8e9  master -> master
       user@USER-PC MINGW64 /f/Workspace_local/git_repo (master)


My commits are pushed to my On cloud git repo successfully......

ON PREMISES:

To setup Git server within our Organization premises, we need a Windows/Unix server. Download and copy the files to the server. 

We access our Git server through browser, which means Gitblit is a Web application. To deploy a web application, we need Tomcat and to deploy war files we need JAVA.

So, I copied Tomcat and gitblit.war file to the server...

root@ubuntu-kvm:~# cd /home/ubu/
root@ubuntu-kvm:/home/ubu#
root@ubuntu-kvm:/home/ubu# ls
Desktop    Downloads  jenkins.war  Pictures  Templates
apache-tomcat-7.0.75.tar.gz  Documents  git.war    Music        Public    Videos
root@ubuntu-kvm:/home/ubu#

gunzip it....


root@ubuntu-kvm:/home/ubu# tar -zxf apache-tomcat-7.0.75.tar.gz 
root@ubuntu-kvm:/home/ubu# ls
apache-tomcat-7.0.75         Desktop    Downloads  jenkins.war  Pictures  Templates
apache-tomcat-7.0.75.tar.gz  Documents  git.war    Music        Public    Videos
root@ubuntu-kvm:/home/ubu#

root@ubuntu-kvm:/home/ubu# cd apache-tomcat-7.0.75/
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75#

root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75# cd webapps/
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/webapps# cp ../../git.war . 
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/webapps# ls
docs  examples  git.war  host-manager  manager  ROOT
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/webapps#

root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/webapps# cd ../bin/
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/bin# ls
bootstrap.jar       commons-daemon-native.tar.gz  digest.sh         startup.bat           tool-wrapper.sh
catalina.bat        configtest.bat                setclasspath.bat  startup.sh            version.bat
catalina.sh         configtest.sh                 setclasspath.sh   tomcat-juli.jar       version.sh
catalina-tasks.xml  daemon.sh                     shutdown.bat      tomcat-native.tar.gz
commons-daemon.jar  digest.bat                    shutdown.sh       tool-wrapper.bat
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/bin#

To start the Tomcat server....

root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/bin# ./startup.sh
Using CATALINA_BASE:   /home/ubu/apache-tomcat-7.0.75
Using CATALINA_HOME:   /home/ubu/apache-tomcat-7.0.75
Using CATALINA_TMPDIR: /home/ubu/apache-tomcat-7.0.75/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /home/ubu/apache-tomcat-7.0.75/bin/bootstrap.jar:/home/ubu/apache-tomcat-7.0.75/bin/tomcat-juli.jar
Tomcat started.
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/bin#




During the start of Tomcat server, war files will be deployed.... git folder was generated...



root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/bin# cd ../webapps

root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/webapps# ls
docs  examples  git  git.war  host-manager  manager  ROOT
root@ubuntu-kvm:/home/ubu/apache-tomcat-7.0.75/webapps#

Enter URL along with "git" to access git.... Login username: admin and password: admin


Let us create new repo on premises through gitblit.... Choose name and type, then Create...



Use the URL which is generated for our repo to checkout the git repo.

user@USERS-PC MINGW64 /f/Workspace_local $ git clone ssh://admin@10.0.0.229:29418/git_onp_repo.git
                Cloning into 'git_onp_repo'...
                The authenticity of host '[10.0.0.229]:29418 ([10.0.0.229]:29418)' can't be established. RSA key fingerprint is SHA256:UqXvhPBG8s2CQAgJTc0IR5oqzAzJQv0mM7j+OgKcgng.0
                Are you sure you want to continue connecting (yes/no)? yes
                Warning: Permanently added '[10.0.0.229]:29418' (RSA) to the list of known hosts.
                Password authentication
                Password:
                remote: Counting objects: 3, done
                remote: Finding sources: 100% (3/3)
                remote: Getting sizes: 100% (2/2)
                remote: Compressing objects: 100% (72/72)
                remote: Total 3 (delta 0), reused 0 (delta 0)
                Receiving objects: 100% (3/3), done.
        user@USERS-PC MINGW64 /f/Workspace_local $

        user@USERS-PC MINGW64 /f/Workspace_local  $  ls
                antivir.txt  git_onp_repo/  git_repo/  hp_sr.txt  subversion/
        user@USERS-PC MINGW64 /f/Workspace_local $

        user@USERS-PC MINGW64 /f/Workspace_local $ cd git_onp_repo/
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
                README.md
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Create a new file ....

        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ tee -a first_file
                hi this is my first file thru git
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ ls
                first_file  README.md
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Check the status and add it...

        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
                On branch master
                Your branch is up-to-date with 'origin/master'.
                Untracked files:
                (use "git add <file>..." to include in what will be committed)
                        first_file
                nothing added to commit but untracked files present (use "git add" to track)
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git add .
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
                On branch master
                Your branch is up-to-date with 'origin/master'.
                Changes to be committed:
                (use "git reset HEAD <file>..." to unstage)
                        new file:   first_file
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

Lets commit this..



        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git commit -m "1st file"
                [master e4c2887] 1st file
                Committer: USERS   <USERS  >
                Your name and email address were configured automatically based
                on your username and hostname. Please check that they are accurate.
                You can suppress this message by setting them explicitly. Run the
                following command and follow the instructions in your editor to edit
                your configuration file:
                           git config --global --edit
                After doing this, you may fix the identity used for this commit with:
                           git commit --amend --reset-author
                1 file changed, 0 insertions(+), 0 deletions(-)
                create mode 100644 first_file
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git status
                On branch master
                Your branch is ahead of 'origin/master' by 1 commit.
                (use "git push" to publish your local commits)
                nothing to commit, working tree clean
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $

To push these changes to remote repo...

        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $ git push
                        Password authentication
                        Password:
                        Counting objects: 3, done.
                        Delta compression using up to 4 threads.
                        Compressing objects: 100% (2/2), done.
                        Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done.
                        Total 3 (delta 0), reused 0 (delta 0)
                        remote: Updating references: 100% (1/1)
                        To ssh://10.0.0.229:29418/git_onp_repo.git
                              c5ef999..e4c2887  master -> master
        user@USERS-PC MINGW64 /f/Workspace_local/git_onp_repo (master) $






In this post we covered installing and deploying gitblit server. Committed few files to both local and remote repo, also both in on cloud and on premises git server...

################################################################################

No comments:

Post a Comment