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.