Showing posts with label VersionControl. Show all posts
Showing posts with label VersionControl. Show all posts

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, 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.

Wednesday, 22 June 2016

Version Control - 05.GitHub_Passwordless_Push

SSH keys to push your commits without asking for password:


     While working with your repositories on GitHub you commit your changes locally. And once you feel you are good with the changes then you will push all the local commits to GitHub. But on thing which annoys you at this moment is it will ask for the password everytime. 

     So, in such cases you can use the SSH-RSA keys to push your changes with out being asked for password.

     Below is the complete process of doing that.


Step 1: Go to ".ssh" dir located in your homedir
Generate an RSA key
# ssh-keygen -t rsa
Now you will have "id_rsa.pub" file.
Copy the contents of it.
Step 2:
Login to your GitHub account
Click on your account and then Click on the Settings button in the right most corner
Select "SSH and GPG keys"
Click on "New SSH Key"
Paste the copied content of "id_rsa.pub" there.
Step 3:
Go to your repository on your host or VM
Configure your Git URL
git remote set-url origin git+ssh://<username>@github.com/<accountname>/<repo_name>.git

You are all set now. Try to push your commits to the remote repo with out giving the password.

Saturday, 19 December 2015

Version Control - 04.GitHub_Pull&Fork

GitHub Pull & Fork

Git Pull Request


When ever we forked another users repository and if we want to collaborate with them we can generate a pull request. After forking and cloning to our local computer if we do changes or some new codes have been added to the repository locally the same will be taking effect to the main repository as well. If we need these changes to made permanent to that repository as well then we need to generate a Pull request. Which will be notified to the owner of the main repository. After reviewing that person can accept or reject that request. If that request has been accepted then our changes will be merged to the main repository and committed.

To do this we need to login to our git account in the WebUI after that click on the "Pull Request" button and there after click on "Create Pull Request" button enter some info regarding the pull and click on "Send Pull Request" button.


Updating your Fork


It is also important to know why should we update our forks. Because so many users might be collaborating to the repository you are working and making changes on. In such case you might not know what the changes that have done by the other users. So if we have enabled upstream then every user can know what the other users have done to that repository. As enabling upstream will upload the changes made on their forks will let the other users know the changes.

Adding remote for upstream

# git remote add upstream <PATH_TO_REPO>
# git remote add upstream https://github.com/vmsnivas/bss.git

Fetch the Changes

# git fetch upstream

Merge them into master

# git merge upstream/master master 

Push the changes

# git push origin master

Friday, 18 December 2015

Version Control - 03.GitHub_Forks

Git Forks


We might be seeing lots of repositories on github from other users. Most
of the users will secure their repositories. In that case we can't add -
or modify code in their repositories. In such cases we need to fork the-
ir repositories and can make changes. On forking someone's repositories 
we will have a copy of their repositories into our account.

How to Fork?
We need to login to the GitHub WebUI and go to a specific repos-
itory of a person that you want. In the right most area you can a Fork 
button. On clicking on that button you can get a copy of that repository
into your account.

After forking that repository you can clone that repository locally fro-
m your account and you can modify those codes as you need.

Ex : We have 2 users namely USER1 and USER2. User2 want some code from 
User1's repository. First User2 need to go to the WebUI and after to th-
e repository needed. 

https://github.com/user1/repository

Click on the fork button. Once the fork completes then he can have that
repository in his account.

https://github.com/user2/repository

Now he can clone to his local system.

# git clone https://github.com/user2/repository.git

Here we need to know one thing that user2 can even clone user1's reposi-
tory but he can't push the changes until he is permitted to do so.

Tuesday, 15 December 2015

Version Control - 02.GitHub_Configs

Configuring your GitHub


Before working with GitHub we need to know that we can make the changes at 3 different levels.
 1. Local
While making the changes locally those changes will take effect to the local user or for a single repo
by which we configured git.
 2. System
If we make any changes at a system level then those changes will take effect to all the users
on that system.
 3. Global
If we make any changes at a global level then those changes will take effect to the repository
globally. In the sense those will be effected to the repository in our global account at Git.

USAGE
git config [<file-option>] [type] [-z|--null] name [value [value_regex]]
git config [<file-option>] [type] --add name value
git config [<file-option>] [type] --replace-all name value [value_regex]
git config [<file-option>] [type] [-z|--null] --get name [value_regex]
git config [<file-option>] [type] [-z|--null] --get-all name [value_regex]
git config [<file-option>] [type] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
git config [<file-option>] [type] [-z|--null] --get-urlmatch name URL
git config [<file-option>] --unset name [value_regex]
git config [<file-option>] --unset-all name [value_regex]
git config [<file-option>] --rename-section old_name new_name
git config [<file-option>] --remove-section name
git config [<file-option>] [-z|--null] [--name-only] -l | --list
git config [<file-option>] --get-color name [default]
git config [<file-option>] --get-colorbool name [stdout-is-tty]
git config [<file-option>] -e | --edit

We will have several configs like configuring the username, user email address, our terminal appearence
settings and configuring some alias to the commands we wish. These changes will be stored in the .git/config
file. We can do the same thing by editing this file too.

1. Configuring a user name
It is important to configure a username as if we do any commit then everyone using that repository
can know that who made the changes. If we don't the changes will be committed as the user we locally
login with.

# git config --global user.name "<FULL NAME>"
Ex : # git config --global user.name "Arjun Shrinivas"

2. Configuring user's email address

# git config --global user.email emailid@domain.com
# git config --global user.email vmsnivas@gmail.com

3. Configure your default editor for Git
This is the default editor used to edit our git configs.

# git config --global core.editor "/bin/vi"

After doing this try to use the below command to edit your git config. Now you will be able to use
vi editor to edit your git config. In this way you can use any editor. All you need to do is you
should specify the complete path of the editor. You can get it by using like "which vi"

# git config --global --edit

4. Configure alias commands
We can also configure some aliases to use them instead of running some long commands.

# git config --global alias.s "status s" --> Shows the status in silent mode
# git config --global alias.lg "log --oneline --decorate --all --graph"

5. Colorizing display for all users of the system

# git config --global color.ui true

6. Configure Carriage return / Line feed (crlf)
If it is true then it will adds the CRs back in when you check files out to the working directory,
making them easier to edit in some Windows apps which require CRs.

# git config --global core.autocrlf true
7. Configure Push Default
Matching - Pushes all matching branches up to GitHub
Simple - Just pushes current branch up to GitHub

# git config --global push.default simple

8. Configure Pull Default

# git config --global pull.rebase true

9. Configure Reuse Recorded Resolution (ReReRe)
This will records all fixes to merge conflicts and reuses them automatically if the same conflict recurs.

# git config --global rerere.enabled true

10. Configure Git URL

# git remote add -t <BRANCH> -f origin https://<username>@github.com/<username>/<repository>.git
# git remote add -t master -f origin https://vmsnivas@github.com/vmsnivas/bss.git

11. List you Git configs

# git config --global --list

Tuesday, 8 December 2015

Version Control - 01.GitHub_Intro

GitHub Intro


Step 1
Login to github.com and signup for a free account.
If you need a private repository then you need to signup for paid account.

Step 2
Login to your Linux machine and create a direcotry for git
# mkdir /git

Step 3
Configure your username and email. So that if someone did any commit then everyone will know who have made it.
# git config --global user.name "User Name"
# git config --global user.email <email_id>

Step 4
Go to that directory and create a new directory for your repository if you
have multiple branches within the repository in your git account.
Else you can add your repository directly.
Case 1: Repository with default branch or a single branch.
Go to your git directory
# cd /git
Update the origin of your repository
# git remote add -t <Branch_Name> -f origin https://<username>@github.com/<username>/<Repo_name>.git
# git remote add -t master -f origin https://vmsnivas@github.com/vmsnivas/ansible.git
Clone your repository now.
# git clone https://vmsnivas@github.com/vmsnivas/ansible.git
Case 2: Repository with multiple branches.
Go to your git directory.
# cd /git
Create a direcotry with the name of the repository and child direcotries with the name of the branches you have.
# mkdir -p <repo_name>/{branch1,branch2,....}
# mkdir -p python/{master,notes,programs}
Go to the repo dir and initialize git.
# cd python
# git init
Set the origin of your repo.
# git remote set-url origin https://<username>@github.com/<username>/<Repo_name>.git
# git remote set-url origin https://vmsnivas@github.com/vmsnivas/python.git
Go to one of your branches and initialize git.
# cd master
# git init
Update the origin of your branch
# git remote add -t branch1 -f origin https://<username>@github.com/<username>/<Repo_name>.git
# git remote add -t master -f origin https://vmsnivas@github.com/vmsnivas/python.git
Now clone the contents of that specific branch from the repo.
# git clone -b branch1 https://<username>@github.com/<username>/<Repo_name>.git
# git clone -b master https://vmsnivas@github.com/vmsnivas/python.git

Click here to view in GitHub