Friday 30 August 2013

Git branching and merging

1) git branch
This will list all the branches of your repository in your local.

2) git branch branchname
To create a new branch of your current working repository. Type following code:-

Eg:- git branch newbranch

This will create a new branch in your local..

3) git checkout
This command will switch you to the branch name you entered.

Eg:-git checkout newbranch

4) git checkout -b branchname
This will create a new branch and will switch you to the same.

5) git push origin branchname
Push the branch from local to the github repository.

6) git merge name-of-the-branch-to-be-merged
To merge two branches first checkout the branch to which you want to merge.

Suppose you want to merge the changes from branch A to branch B then do the following:
git checkout B
git merge A

7) git stash
Sometimes you want to switch to some other branch without committing and pushing the changes of the current branch. However you still want to save the changes you have done in that branch. This can be done with the help of stashing.

Suppose you are in branch A and you want to save its changes before switching to branch B.
git stash
This will save the status of the current working directory.

Now you can switch to other branch suppose B
git checkout B
Do the changes, commit it and push it.

Now switch back to branch A
git checkout A
git stash apply //to apply the status u saved before.
git stash apply --index //to reapply the staged changes





Tuesday 27 August 2013

Git Basics

Today I am going to share some of the basic commands of the git.
Starting with a brief description about git :

In software development, Git is a distributed version control and source code management system with an emphasis on speed. Read More
Source:wikipedia

Now coming to commands,I am not going to explain each and every command in detail as the git documentation is enough for that. Will only give brief introduction about them

1) Create your account in github

Goto https://github.com and create your account.

2) Create a repository

Create an empty repository where you want to push the data from your local machine.

Give a name to the repository
A url will be generated for your respository. This is useful in the later process while adding a remote in your local for pushing and pulling purpose. 



3) git init

This command creates an empty git repository.
Suppose you have a folder named "GitTest" in your local. Now you want to push this folder to github then go inside that folder using command line and type "git init".

3) Now add the git remote you created in github.com in your local so that the local changes are pushed to that url .

 git remote add <short name for the url> <url>
 eg : - git remote add origin https://github.com/nikitasingh/GitTest.git

Here origin is the short name for the url

git remote -v will list all the remotes which are added for your repository.

5) Now create some file like "xyz.txt" inside that folder. (Suppose this is the file you want to push to the github)

6) git add

To add the newly created file type-

git add xyz.txt

To add all the files in your folder to github type:-

git add .

7) git commit

File you just added is not ready to be pushed yet, If you will run the git push command without running commit command this file will not be added to your github repository.

so to commit this file type-

git commit -m "first file added" xyz.txt

Here -m is used to give commit messages, you cant run commit command without giving a commit message

8) git push

The next step is to push the changes to the github repository from your local machine. To do that type:

git push origin master

It will ask your login credentials to push the changes.

9) git pull

git pull is used to fetch the changes from the github repository to your local machine.
When two or more people are working on the same repository, it is must to pull the changes so that you have the updated project in your local.

9) git status

If you will type git status it will show the files with following details:-

  •  unmodified files
  •  modified files
  •  added files
  • deleted files
  • renamed files
  • copied files
  • untracked files

Monday 12 August 2013

Difference between collect, select, map and each in ruby

This post is not related to rails part but the RUBY part.

To iterate over an array we generally use collect, select, map and each. As I am beginner I never actually thought what is the difference and I kept using EACH for every loop iteration. However, recently I researched a bit more about these loops and thought to share it . It might help some one I guess  :)

1) Map

Map takes the enumerable object and a block like this [1,2,3].map { |n| n*2 } and evaluates the block for each element and then return a new array with the calculated values.

so the outcome of  [1,2,3].map { |n| n*2 } will be [2,4,6]

If you are try to use map to select any specific values like where n >2 then it will evaluate each element and will output only the result which will be either true or false

so the outcome of  [1,2,3].map { |n| n>2 } will be [false, false, true]

2) Collect

Collect is similar to Map which takes the enumerable object and a block like this [1,2,3].collect { |n| n*2 } and evaluates the block for each element and then return a new array with the calculated values.

so the outcome [1,2,3].collect{ |n| n*2 } of will be [2,4,6]

If you are try to use collect to select any specific values like where n >2 then it will evaluate each element and will output only the result which will be either true or false

so the outcome of  [1,2,3].collect { |n| n>2 } will be [false, false, true]

3) Select

Select evaluates the block with each element for which the block returns true.

so the outcome of  [1,2,3].select { |n| n*2 } will be [1,2,3]

If you are try to use select to get any specific values like where n >2 then it will evaluate each element but returns the array that meets the condition

so the outcome of  [1,2,3].select{ |n| n>2 } will be [3]

4) Each

Each will evaluate the block with each array and will return the original array not the calculated one.
so the outcome of  [1,2,3].each{ |n| n*2 } will be [1,2,3]

If you are try to use each to select any specific values like where n >2 then it will evaluate each element but returns the original array

so the outcome of  [1,2,3].each { |n| n>2 } will be [1,2,3]