Friday 11 November 2016

RY_01_Ruby_and_Rails_Installation

Ruby on Rails Installation Process

We are using a Ubuntu machine for this installation and practice.

Install Ruby on Rails using "rbenv" on Ubtuntu:

  • Install the required packages and pre-requisites:
    sudo apt-get update -y
    sudo apt-get install -y git gitk
    sudo apt-get install -y gcc build-essential libpq-dev libssl-dev libreadline-dev libsqlite3-dev zliblg-dev
  • Clone the rbenv repository:
    git clone git://github.com/sstephenson/rbenv.git /GitHub/ruby/.rbenv
  • Export the RECIPE PUPPY site with port to bashrc:
    echo 'export RECIPEPUPPY_HOSTPORT=www.recipepuppy.com:80' >> ~/.bashrc
  • Export rbenv/bin:
    echo 'export PATH="$PATH:/GitHub/ruby/.rbenv/bin"' >> ~/.bashrc
    echo 'eval "$(rbenv init .)"' >> ~/.bashrc
  • Now clone the ruby-build repository:
    git clone git://github.com/sstephenson/ruby-build.git /GitHub/ruby/.rbenv/plugins/ruby-build
  • Set path for this plugins directory:
    echo 'export PATH="$PATH:/GitHub/ruby/.rbenv/plugins/ruby-build/bin"' >> ~/.bashrc
  • Now we need to source the bashrc as we have some entries into it.:
    source ~/.bashrc
  • Lets begin the installation now:
    rbenv install -v 2.2.2
  • Lets set the default version of Ruby:
    rbenv global 2.2.2
    ruby -v
  • We need to stop the gems to generate local documentation:
    echo "gem: --no-document" > ~/.gemrc
  • Install the Ruby gem manager:
    gem install bundler
  • Install Rails:
    gem install rails -v 4.2.3
  • Now we need to rehash as it will let us to get command completion which relates to rails.
    rbenv rehash
  • Let us install some other packages which are needed in our practice
    sudo apt-get install -y software-properties-common python-software-properties
  • Now let us install some javascripts through a custom location
    sudo add-apt-repository ppa:chris-lea/node.js
    sudo apt-get install -y node.js
    sudo apt-get install -y bzip2
  • Let us setup path for some ENV
    echo 'export PHANTOM_JS="phantomjs-1.9.8-linux-x86-64"' >> ~/.bashrc
  • Let us download and install the PhantomJS package:
    cd /tmp
    curl -L https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2 | tar -xjvf
    sudo mv $PHANTOM_JS /usr/local/share
    sudo ln -s /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin
    phantomjs -v
  • Install text editor:
    curl http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%202.0.2%20x64.tar.bz2 | tar -xjf
    sudo mv 'Sublime Text 2' /opt/SublimeText2
    echo 'export PATH=$PATH:/opt/SublimeText2' >> ~/.bashrc
    source ~/.bashrc
  • Let us check all the versions now
    git --version
    phantomjs -v
    ruby -v
    rails -v
  • Now let us create a rails application
    Create a project directory and then create a application
    mkdir /u01/ruby/projects &&cd /u01/ruby/projects

    rails new test-install -q
NOTE: Here we are using -r to run this command in quite mode as it will produce lot of output.
  • Now cd into the new rails application we have created and run the server
    cd test-install ; rails server
  • Let us check the application in the browser
    http://localhost:3000
  • Now we have completed configuring ruby on rails.

Thursday 10 November 2016

CI-CD_005_Git_with_Jenkins

Integrating Git with Jenkins

If we are dealing with some specific code based builds where the code doesn't need any modification can be directly used for a build. But in some cases the code need to be changed several times depending on the need. In such case the periodic builds need to have those modifications. We use SCM (Source Code Management) to overcome this situation. And instead of manually copying the code to the build we can integrate the repositories from the SCM tools with our Jenkins builds.

Configuration

  • Global Settings In the Configure System page, the Git Plugin provides the following options:
    • Global Config user.name Value: if provided git config user.name is called before builds. This can be overridden by individual projects.
    • Global Config user.email Value: if provided git config user.email is called before builds. This can be overridden by individual projects.
    • Create new accounts base on author/committer's email: if checked, upon parsing of git change logs, new user accounts are created on demand for the identified committers / authors in the internal Jenkins database. The e-mail address is used as the id of the account.

Project Configuration

At the project level the Git Plugin is configured by selecting the Git option at the Source Code Management section.
The main section is Repositories where several can be configured. The information to provide include:
  • The Repository URL, which is mandatory. The URL uses the same syntax as the git clone command. This can be a URL or a local file path. Note that for super-projects (repositories with submodules), only a local file path or a complete URL is valid.
    • The following are examples of valid git URLs.
      ssh://git@github.com/github/git.git
      git@github.com:github/git.git (short notation for ssh protocol)
      ssh://user@other.host.com/~/repos/R.git (to access the {repos/R.git repository in the user's home directory)
      https://github.com/github/git.git
      git://github.com/github/git.git
    • If the repository is a super-project, the location from which to clone submodules is dependent on whether the repository is bare or non-bare (i.e. has a working directory).
      • If the super-project is bare, the location of the submodules will be taken from .gitmodules.
      • If the super-project is not bare, it is assumed that the repository has each of its submodules cloned and checked out appropriately. Thus, the submodules will be taken directly from a path like ${SUPER_PROJECT_URL}/${SUBMODULE}, rather than relying on information from .gitmodules.
    • For a local URL/path to a super-project, git rev-parse --is-bare-repository is used to detect whether the super-project is bare or not.
    • For a remote URL to a super-project, the ending of the URL determines whether a bare or non-bare repository is assumed:
      • If the remote URL ends with /.git, a non-bare repository is assumed.
      • If the remote URL does NOT end with /.git, a bare repository is assumed.
  • Credentials: Credentials to use to connect to the repository (unless anonymous access is allowed), using the Jenkins Credentials Management functionality. The type of credentials used depends on the underlying protocol. For SSH connections only private key authentication is supported.
The next section is Branches to Build in which several branch specifiers can be provided. For each of these specs, leaving it blank means that all branches will be examined for changes and built. The safest way is to use the refs/heads/ syntax. This way the expected branch is unambiguous. See the online help for more options.
A Repository Browser can also be configured, which adds links in "changes" views within Jenkins to an external system for browsing the details of those changes. The "Auto" selection attempts to infer the repository browser from other jobs, if supported by the SCM and a job with matching SCM details can be found, though it can also be selected manually.
Finally, the Git Plugin is extensible and the plugin itself as well as external plugins can provide Additional Behaviours to tweak the SCM configuration inside each particular project. Please refer to the online help of each of the additional options for further information.

Using Extra Repositories

Since GIT is a Distributed SCM, it is possible in the Advanced section to specify multiple repositories. You may wish to do this to, for example, pull all in-progress work from individual developers machines, and pre-test them before they are committed to a centralised repository - this way developers may get an early warning that a branch in progress may not be stable.
The GIT plugin will make reasonable attempts to try and pull submodule states from distributed repositories, with the proviso that this feature is not currently well supported within GIT itself.

Wednesday 9 November 2016

CI-CD_004_Jenkins_Parameterized_Builds

Parameterized Builds

Sometimes, it is useful/necessary to have your builds take several "parameters." Consider the following use case:
  • You set up a test job on Jenkins, and it accepts a distribution bundle as a parameter and perform tests against it. You want to have developers do local builds and let them submit builds for test execution on Jenkins. In such a case, your parameter is a zip file that contains a distribution.
  • Your test suite takes so much time to run that in normal execution you can't afford to run the entire test cycle. So you want to control the portion of the test to be executed. In such a case, your parameter is perhaps a string token that indicates that test suite to be run.
These parameters are available as environment parameters. For example like we use the variables in a Shell script. Here we will have two parameters to be specified which are
1. Name
2. Default Value
If we are using any Shell script for our build and we have some variables need to be specified as arguments for the script then we need to keep the calling name of the variable as "Name" for the build and if we need to use default values for that we can specify those in the "Default Value" column. Or else we need to provide the value while running a build. Those values will be directly assigned to the variable parameters which will be used in the script.
For having our project to use parameters we need to do the following.
After the Project name and Description we'll have a check box "This build is parameterized". We need to check that box enabled to run parameterized builds. Now we need to provide the name and default value to be used within the project.

Tuesday 8 November 2016

CI-CD_003-Jenkins-HipChat-Notifications

Notify your team about the build with status in HipChat

  • To have this feature you need to install the HipChat Plugin.
    * Go to "Manage Jenkins"
    * Click on "Manage Plugins"
    * Click on "Available tab"
    * In the filter search for "HipChat"
    * Select "HipChat Plugin" and click on "Install without restart"
  • Now restart your jenkins. You can do it depending on the way using jenkins.
    • If you are using jenkins directly then
        * service jenkins restart
    
    • If you are using jenkins through tomcat then
        * Login to your tomcat webui, go to "Manager App" and click on "Reload Jenkins"
            (or)
        * http://<HOSTNAME/IP>:8080/jenkins/restart
    
  • Now you need to login to your HipChat web-ui and go to the room on which you have Admin access.
NOTE : To get admin access you need to create a room. You will be admin of the room that you've created.
    * Login to your hipchat account ( https://<my-hipchat>.hipchat.com/rooms/ )
        -- Ex: https://linux-library.hipchat.com/rooms/
    * Find and click on the room you've created.
    * Click on "Tokens"
    * Under the "Create new token" section provide a lable for your notification message.
    * Select "Send Notification" from "Scopes"
    * Click on Create.

    -- Above the "Create new token" section you'll get the Token with the lable you've give.

    * Copy that token.

    -- Now in the url after "tokens/" you'll notice a number that is your room number.

    * Copy that number.
  • Now you need to configure these details in Jenkins.
    * Go to your jenkins home page click on "Manage Jenkins"
    * Click on "Configure System"
    * Go to "Global HipChat Notifier Settings" and provide the following settings
        * HipChat Server    : api.hipchat.com
        * Use v2 API        : yes
        * API Token     : <Paste your token here>
        * Room          : <Paste your room-id here>
    * Click Save
  • Now in your project add the "Post-build Actions"
    * Click on "Add post-build action"
    * Select "HipChat Notifications" and provide the following details
        * Project Room      : <Paste your room-id here>
    * Under message templates provide the following values to get your build status notification.
        * Job started   :   ${JOB_NAME} #${BUILD_NUMBER}  ---  $STATUS  ---  ${URL}
        * Job completed :   ${JOB_NAME} #${BUILD_NUMBER}  ---  $STATUS - $DURATION  ---  ${URL}
  • Now start the build and verify the build status notification in HipChat

Monday 7 November 2016

CI-CD_002-Jenkins-on-Tomcat

Tomcat installation

  • Verify the Java installation
    # java -version
  • Set the Java home and Path
    # echo "export JAVA_HOME=/usr/java/jdk1.8.0_91/" >> ~/.bashrc
    # echo "export JAVA_HOME=/usr/java/jdk1.8.0_91/" >> /etc/bashrc
    # echo "export PATH=$PATH:$JAVA_HOME/bin/" >> /etc/bashrc
    # echo "export PATH=$PATH:$JAVA_HOME/bin/" >> ~/.bashrc
  • Download tomcat application from the following site in tar.gz format
    https://tomcat.apache.org/download-70.cgi
  • Make a custom application directory and untar the tomcat application
    mkdir /u01
    cp -prv ~/Downloads/apache-tomcat-7.0.69.tar.gz /u01/
    cd /u01
    tar -xzvf apache-tomcat-7.0.69.tar.gz
  • Create a soft link for the unzipped directory as tomcat
    ln -s apache-tomcat-7.0.69 tomcat
  • Copy the jenkins.war file into webapps directory
    cp -prv `locate jenkins.war` /u01/tomcat/webapps/
  • Now clone the "Jenkins" repo from Linux-Library git account "vmsnivas" and copy the tomcat script from the scripts dir to /sbin
    mkdir /git
    cd /git
    git clone https://github.com/vmsnivas/jenkins.git
    cp -prv /git/jenkins/scripts/tomcat /sbin/
  • Now start the tomcat application
    tomcat start
NOTE : Here you'll face some problems as the default ports of Jenkins and Tomcat are the same (8080). So we'll change the Jenkins port to some other in my case i am using 18080. Also will change the hostname, users and roles in tomcat
  • To change the default port of Jenkins edit "/etc/sysconfig/jenkins"
    JENKINS_PORT="18080"
  • Now let us configure the Tomcat
    - Change the hostname from "localhost" to "dev1.linux-library.ll"
        <Engine name="Catalina" defaultHost="dev1.linux-lirary.ll">
        <Host name="dev1.linux-library.ll"  appBase="webapps"
  • Configure a user to manage the UI. Edit "/u01/tomcat/conf/tomcat-users.xml" and add the following above "" tag.
    <role rolename="manager-gui"/>
    <role rolename="tomcat"/>
    <user username="arjun" password="aDminTom" roles="tomcat,manager-gui"/>
  • Now you can be able to use Jenkins through Tomcat. All you need to do is to load the below url
    http://dev1.linux-library.ll:8080/jenkins/

Friday 28 October 2016

Version Control - 08.02 GIT-ADD

Do you have multiple modifications or additions to your repository?

Facing trouble to track those?

Thinking to have individual commits?

Are you again thinking it is painful to have those multiple commits?

Below is the script which can make this painful issue fixed.

 #!/bin/bash  
   
 # GIT Alias to status  
 git config --global alias.s "status -s"  
   
 A=`git s | grep '^A\|^AM\|^AR\|^AD^ A' | awk '{print $2}'`  
 U=`git s | grep ^? | awk '{print $2}'`  
 M=`git s | grep '^M\|^MA\|^MR\|^MM\|^MD\|^ M' | awk '{print $2}'`  
 R=`git s | grep '^R\|^RA\|^RM\|^RR\|^RD\|^ R' | awk '{print $4}'`  
 D=`git s | grep '^D\|^ D' | awk '{print $2}'`  
   
 function add {  
      for i in `echo $A`  
      do  
           git add $i  
           git commit -m "Added $i"  
      done  
 }  
   
 function stgnew {  
      for i in `echo $U`  
      do  
           git add $i  
           git commit -m "Added $i"  
      done  
 }  
   
 function modify {  
      for i in `echo $M`  
      do  
           git add $i  
           git commit -m "Modified $i"  
      done  
 }  
   
 function rename {  
      for i in `echo $R`  
      do  
           R1=`git s | grep $i | awk '{print $2}'`  
           R2=`git s | grep $i | awk '{print $4}'`  
           git add $R2  
           git commit -m "Renamed/Moved $R1 to $R2"  
      done  
 }  
   
 function delete {  
      for i in `echo $D`  
      do  
 #          git add $i  
           git commit -m "Removed $i"  
      done  
 }  
   
 if [[ -z $(git s) ]]  
 then  
      echo "There are neither additions nor deletions to stage"  
 else  
      for i in A U M R D  
      do  
           case $i in  
           A)  
                add  
           ;;  
           U)  
                stgnew  
           ;;  
           M)  
                modify  
           ;;  
           R)  
                rename  
           ;;  
           D)  
                delete  
           ;;  
           esac  
      done  
      git push -fu origin master  
 fi  
   
 # END  

Sunday 16 October 2016

CI-CD_001-Jenkins-Installation

Installation process of Jenkins

  • Download and install latest version of JDK (Java 7 or newer version is recomended)
  • Add the Jenkins repository to the yum repos, and install Jenkins from here.
    $ sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
    $ sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
    $ sudo yum install jenkins
  • Turn jenkins service at startup and restart the service
    # chkconfig jenkins on
    # service jenkins start
  • Open your browser and hit the below URL to open Jenkins UI
    http://localhost:8080/
  • It will now prompt for the Initial Admin password which is available in the below file
    /var/lib/jenkins/secrets/initialAdminPassword
Copy that password and paste in the field in the Jenkins web UI
  • Now it will prompt for the installation of Jenkins Plugins. Go for the default.
  • You are all set and the final step is that you need to create a Jenkins user.

Sunday 28 August 2016

Version Control - 09.GIT-CLONE

Hello geeks, 


  • Do you have multiple git accounts, organisations and numerous number of repositories? 
  • Worrying to know how to memorise the urls?
  • Felling exhausted to clone your repositories into your machine to work?
  • Again thinking this lengthy painful story through all the machines you work on in your day-to-day?
Don't worry here is the script i have written to accomplish the git repositories cloning task and to make us feel light. Let us know about how this works and what it will do.

It has 2 functions.
- One checks your git login account to find the repositories
- Another one checks your Org account and also does the same

Once it found some repositories then it will create separate directories for users and orgs. There after it will pull all the contents of that repository to the local machine.


 #!/bin/bash
 clear  
 read -p "Input your GitHub login name: " USER  
   
 read -s -p "GitHub login password: " PASSWORD  
   
 echo  
 read -p "Input your GitHub org name: " ORG  
   
 echo  
 read -p "Input your fullname: " FULLNAME  
 echo "Configuring Git user on $HOSTNAME"  
 git config --global user.name "$FULLNAME"  
 read -p "Input your email: " EMAIL  
 echo "Configuring Git email on $HOSTNAME"  
 git config --global user.email "$EMAIL"  
   
 echo  
 for name in $USER $ORG  
 do  
      if [[ ! -z $USER ]] && [[ ! -d /GitHub/users/$USER ]]  
      then  
           mkdir -p /GitHub/users/$USER/  
      fi  
      if [[ ! -z $ORG ]] && [[ ! -d /GitHub/orgs/$ORG ]]  
      then  
           mkdir -p /GitHub/orgs/$ORG/  
      fi  
 done  
   
 USERREPOS="/GitHub/users/$USER"  
 ORGREPOS="/GitHub/orgs/$ORG"  
   
 UREPOCOUNT=0  
 function userrepos {  
      if [[ ! -z $USER ]]  
      then  
           curl -s -u $USER:$PASSWORD https://api.github.com/users/$USER/repos > res.out  
           URLS=`more res.out | jq '.[] .clone_url'`  
           CLONE_URLS=`echo $URLS | sed -ne 's/https:\/\//git+ssh:\/\/git@/p' | tr -d '"'`  
           for REPO in `echo $CLONE_URLS`  
           do  
                REPONAME=`echo $REPO | awk -F$USER/ '{print $2}' | sed -ne 's/.git//p'`  
                if [[ ! -d $USERREPOS/$REPONAME ]]  
                then  
                     mkdir -p $USERREPOS/$REPONAME  
                     cd $USERREPOS/$REPONAME  
                     git init >> usercloneres.out 2>&1  
                     git remote add -f origin $REPO >> usercloneres.out 2>&1  
                     git remote set-url origin $REPO >> usercloneres.out 2>&1  
                     git pull -ff origin master >> usercloneres.out 2>&1  
                     UREPOCOUNT=`expr $UREPOCOUNT + 1`  
                     echo -e "Cloned $USER/$REPONAME successfully!"  
                else  
                     echo "Ignoring $USER/$REPONAME as repository exists!"  
                fi  
           done  
      fi  
 }  
   
 OREPOCOUNT=0  
 function orgrepos {  
      if [[ ! -z $USER ]] && [[ ! -z $ORG ]]  
      then  
           curl -s -u $USER:$PASSWORD https://api.github.com/orgs/$ORG/repos > res.out  
           URLS=`more res.out | jq '.[] .clone_url'`  
           CLONE_URLS=`echo $URLS | sed -ne 's/https:\/\//git+ssh:\/\/git@/p' | tr -d '"'`  
           for REPO in `echo $CLONE_URLS`  
           do  
                REPONAME=`echo $REPO | awk -F$ORG/ '{print $2}' | sed -ne 's/.git//p'`  
                if [[ ! -d $ORGREPOS/$REPONAME ]]  
                then  
                     mkdir -p $ORGREPOS/$REPONAME  
                     cd $ORGREPOS/$REPONAME  
                     git init >> orgcloneres.out 2>&1  
                     git remote add -f origin $REPO >> orgcloneres.out 2>&1  
                     git remote set-url origin $REPO >> orgcloneres.out 2>&1  
                     git pull -ff origin master >> orgcloneres.out 2>&1  
                     echo -e "Cloned $ORG/$REPONAME successfully!"  
                     OREPOCOUNT=`expr $OREPOCOUNT + 1`  
                else  
                     echo "Ignoring $ORG/$REPONAME as repository exists!"  
                fi  
           done  
      elif [[ -z $ORG ]]  
      then  
           echo "You have no Git organizations!"  
      fi  
 }  
   
 for OPT in USER ORG  
 do  
      case $OPT in   
      USER)  
 #          :> usercloneres.out  
           userrepos  
           if [[ ! -z $USER ]]  
           then  
                echo -e "Cloned $UREPOCOUNT user repos of $USER successfully.\n"  
           fi  
      ;;  
      ORG)  
 #          :> orgcloneres.out  
           orgrepos  
           if [[ ! -z $ORG ]]  
           then  
                echo -e "Cloned $OREPOCOUNT user repos of $ORG successfully.\n"  
           fi  
      ;;  
      esac  
 done  
   
 echo -e "\n Successfully cloned `expr $UREPOCOUNT + $OREPOCOUNT` repos. \n"  

Saturday 30 July 2016

Version Control - 08.GIT-ADD

We now have some idea on GIT. One of the painful thing is when we or someone keep on modifying the code or files then we need to stage each and every change to have them under version control. If we have some numerous number of additions, deletions or modifications then it would be somehow become painful to manage those. Below script can be used to get everything which is ready to be staged. In this script i am checking the git status and getting the list of files which have been modified and adding the changes on those to stage.

 #!/bin/bash  
 # GIT Alias to status  
 git config --global alias.s "status -s"  
 A=`git s | grep '^A\|^AM\|^AR\|^AD^ A' | awk '{print $2}'`  
 U=`git s | grep ^? | awk '{print $2}'`  
 M=`git s | grep '^M\|^MA\|^MR\|^MM\|^MD\|^ M' | awk '{print $2}'`  
 R=`git s | grep '^R\|^RA\|^RM\|^RR\|^RD\|^ R' | awk '{print $4}'`  
 D=`git s | grep '^D\|^ D' | awk '{print $2}'`  
 function add {  
      for i in `echo $A`  
      do  
           echo "Adding $i to staging..."  
           git add $i  
      done  
 }  
 function stgnew {  
      for i in `echo $U`  
      do  
           echo "Adding $i to staging..."  
           git add $i  
      done  
 }  
 function modify {  
      for i in `echo $M`  
      do  
           echo "Modifying $i and staging..."  
           git add $i  
      done  
 }  
 function rename {  
      for i in `echo $R`  
      do  
           R1=`git s | grep $i | awk '{print $2}'`  
           R2=`git s | grep $i | awk '{print $4}'`  
           echo "Renaming/Moving $R1 to $R2 and staging..."  
           git add $R2  
      done  
 }  
 function delete {  
      for i in `echo $D`  
      do  
           echo "Removing $i ..."  
 #          git add $i  
      done  
 }  
 if [[ -z $(git s) ]]  
 then  
      echo "There are neither additions nor deletions to stage"  
 else  
      for i in A U M R D  
      do  
           case $i in  
           A)  
                add  
           ;;  
           U)  
                stgnew  
           ;;  
           M)  
                modify  
           ;;  
           R)  
                rename  
           ;;  
           D)  
                delete  
           ;;  
           esac  
      done  
 fi  
 # END 

Friday 22 July 2016

Version Control - 07.GitHub_Recovery

Recovering data after deleting data using "git rm" and "rm -rf"

- Some times we might delete some data accidentally or else some times we might delete data as of our choice. But what ever the case might be we might need that data after some time in some cases. So in Such case we can recover the data.

- Let us assume that we deleted abc.txt. Staged, commited and push the changes to git. Then we need to get the   previous commit id at where the file was existing. Now we need to checkout the file to that commit.

- We now have the file recovered. But it is not good with the version control, so let us create a directory, move that file into the newly created directory using "git mv" add it to stage, commit and push it to git.

- List information about a particular filename that has been deleted from a git repository:
  This can be used to get the commit id of the file when it was removed.

git log -- [deleted-filename] --> 

- Now find the commit before the file was removed.

git rev-list -n 1 HEAD -- [deleted-filename]

- Restore the filename to that commit using the commit id by following the below procedure.

git checkout <COMMIT_ID> -- <FILE_NAME>
( or )
git checkout <COMMIT_ID> <FILE_NAME>

- Either of the above command will work to recover the file.

mkdir recover && git mv <FILE_NAME> recover/

- Now stage the changes, commit and push to git.

- While doing the same thing for directories we need to rename the directory after recovering and need to stage, commit ad push to Git.
git checkout <COMMIT_ID> -- <DIR_NAME>/*
( or )
git checkout <COMMIT_ID> <DIR_NAME>/*

NOTE: This procedure can be used when we remove the directory as well.

THE BEST OF THIS PROCESS IS THAT WE CAN RECOVER FILES & DIRECTORIES IN A REPOSITORY IF THOSE HAVE BEEN REMOVED USING "rm -rf".

If we have accidentally removed some files using "rm -rf" within a repository then we just need to get the latest commit ID in that repository and need to checkout that file or directory to that commit ID. This will restores the files which were deleted within a repository even using the dangeroous command "rm -rf".

Friday 8 July 2016

Version Control - 06.GitHub_Branching_and_Merging

Git Branching and Merging

To check what all the branches we have and also on which branch we are:

# git branch

To create a new branch in GIT:

# git branch <branch_name>
Ex: # git branch feature1

To work on it we need to move/checkout to that branch. For easy reference i am using my ex. branch feature1 ahead in this topic.

# git checkout feature1

Now try to chek on which branch you are. You need to notice that you are on feature1

# git branch
NOTE: The branch which you are on should be highlighted with asterik symbol at begining.

Now you can do your work on it. Once you have finished your work then you need to push your changes which have commited on this branch to the same remote branch after of series of commits.

# git push origin feature1

Once you are clear that all the work has completed in this branch then you can merge it with the master branch. For that you need to checkout to the master branch first.

# git checkout master

Now you can merge all the changes in that branch into the master branch.

# git merge feature1

Once you merged the changes into master then you need to push the affected changes on master to remote master.

# git push origin master

If you feel that there is no need of this branch then you can remove that branch. You also need to remove that branch on GitHub too.

# git branch -D feature1
# git push origin :feature1

Congrats, now you are all done. You have all the changes of the new feature branch in the master and also without affecting those you have verified those from the feature branch and have merged into master. After these all you have removed the branch.