Wednesday, July 29, 2015

Git

What is Git

  • Git is software that keeps track of changes that you make to files and directories and it is specially good in keeping keeping track of text changes that you maake.
  • Version control system
  • Source code management system 

Who should use Git ?

Primarily people work with source codes are going to get the most out of Git, but its not just for them its really for anyone who wants to track edits, specially edits for text documents. It is useful for anyone needing to share changes with colaboraters

Installing and Configuring

On ubuntu you can install Git with apt-get install command
$ apt-get install git

To configure with your user name and email address,

$ git config --global user.name <user name>

$ git config --global user.email <your email>


Generating ssh keys

SSH keys are a way to identify trusted computers, without involving passwords. The steps below will walk you through generating an SSH key and adding the public key to your GitHub account.

Step 1: Check for SSH keys

To check the existing SSH keys on your computer
$ ls -al ~/.ssh

Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:

id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub

If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can skip Step 2 and go straight to Step 3.

Step 2:Generate a new SSH key

Try following code with your GitHub email address
$ ssh-keygen -t rsa -b 4096 -C <your email>

and
Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

Then you'll be asked for passpharse
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

well thats it, now it will be given the fingerprint, or id of your ssh key

Step 3: Add your key to the ssh-agent

Ensure ssh-agent is enabled
$eval "$(ssh-agent -s)"

Add your ssh key to the ssh-agent
$ssh-add ~/.ssh/id_rsa

Step 4: Add your SSH key to your account

To configure your GitHub account to use your SSH key:

In your favorite text editor, open the ~/.ssh/id_rsa.pub file.

Select the entire contents of the file and copy it to your clipboard. Do not add any newlines or whitespace.

Add the copied key to GitHub:
In the top right corner of any page, click your profile photo, then click Settings.
In the user settings sidebar, click SSH keys.
Click Add SSH key.

In the Title field, add a descriptive label for the new key. For example, if you're using a personal Ubuntu, you might call this key "Personal ubuntu Desktop".

Paste your key into the "Key" field.

Click Add key.

Step 5: Test the connection

$ ssh -T git@github.com
# Attempts to ssh to GitHub

You may see a warning and then asked to verify the fingerprint, then type yes 
Then you will see this
  
Hi < your username> You've successfully authenticated, but GitHub does not provide shell access.

If the username in the message is yours, you've successfully set up your SSH key

                               ************************************

Ok !! Lets see what we can do with git

To make your current directory as your git local repository, use the command
$ git init

This will tell git, set up this as your homebase. Make this a Git repository and track all the files that come and go changes that are made inside this directory, only the things inside this directory, Git will be aware of it

lets use ls -la to see what is created after git initialization (git init )

You can see folder named .git inside that folder, it is the Git's workspace where Git does everything that it's going to do.

To see what's inside that .git folder just type ls -la .git in the terminal

Then you can make some changes inside that folder, I am going to create simple helloworld.java file inside the folder

Now switch over to terminal and tell Git add all changes that have been made to this entire project
$ git add .

Or you can just mention the name that you've created recently,
    $ git add helloworld.java


if you want to add all the java files then just type
    $ git add *.java

Now I am going to commit that change, tell Git to put it permanent memory. (put it in the repository)
$ git commit -m “inital commit”
after -m you ca give it a message

So this is the basic process that we are going to follow throughout working with Git
  • Make changes
  • Add the changes
  • Commit changes

Use meaningful commit messages, because when we come back later we can just look at the commit message and know what's inside

Viewing the commit log

$ git log

In first line you can see the commit id, which is unique to each commit message, then the author of the commit
If we want to see most recent five
$ git log -n 5

and
$ git log --since=2015-07-28
$ git log –until=2015-07-28
$ git log –author=“sashikau”
$ git log –grep= “Init”

You can easily create your on-line repo by sign in to your GitHub account.
To connect your local repo with the on-line one, you have to use the command :
$ git remote add origin https://github.com/username/repo.git

To view existing remotes
$ git remote -v  

To rename a remote 

$ git remote rename origin destination  
    - Change remote name from 'origin' to 'destination'

To delete a remote   
$ git remote rm destination

So now we are ready to send our files to remote repository, to do that we can use push command

$ git push <REMOTENAME> <BRANCHNAME>

eg : $ git push -u origin master


Fetching a remote

When working with other people's repositories, there are few basic git commands to remember

  • $ git clone
  • $ git fetch
  • $ git merge
  • $ git pull
      
These commands are very useful when interacting with a remote repository. clone and fetch download remote code from a repository's remote URL to your local computer, merge is used to merge different people's work together with yours, and pull is a combination of fetch and merge.


Clone

To grab a complete copy of another user's repository, use git clone like this: 

$ git clone https://github.com/username/repository.git

When you run git clone, the following actions occur:

   * A new folder called repo is made
   * It is initialized as a Git repository
   * A remote named origin is created, pointing to the URL you cloned from
   * All of the repository's files and commits are downloaded there
   * The default branch (usually called master) is checked out

For every branch foo in the remote repository, a corresponding remote-tracking branch refs/remotes/origin/foo is created in your local repository. You can usually abbreviate such remote-tracking branch names to origin/foo.


Fetch 

Use git fetch to retrieve new work done by other people. Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

If you already have a local repository with a remote URL set up for the desired project, you can grab all the new information by using git fetch *remotename* in the terminal:

$ git fetch remotename 
# Fetches updates made to a remote repository

Merge

Merging combines your local changes with changes made by others.Typically, you'd merge a remote-tracking branch (i.e., a branch fetched from a remote repository) with your local branch :

$ git merge remotename/branchname  
# Merges updates made online with your local work
 
Pull

git pull is a convenient shortcut for completing both git fetch and git mergein the same command:

$ git pull remotename branchname
# Grabs online updates and merges them with your local work

Because pull performs a merge on the retrieved changes, you should ensure that your local work is committed before running the pull command. If you run into a merge conflict you cannot resolve, or if you decide to quit the merge, you can use git merge --abort to take the branch back to where it was in before you pulled.

To see the differences, run

$ git diff HEAD

To undo all the changes since the last commit

$git checkout --myclass.java  

To remove a file from git just type

$git rm myclass.java 


Branches


To create a branch
$ git branch <branch name>
branch name with the asterisk sign is the selected branch at the moment

To see the existing branches
$ git branch

To view all of them just add -a (including remote branches)
$ git branch -a

To swich to a different branche
$ git checkout <beanch name>

Similarly you can switch to master
$ git checkout master

You can merge your branch with the master
$ git merge <branch name>

Once you are done with the branch you can easily remove it
$ git branch -d my_branch


What is cherry-pick?

cherry-pick allows you to merge single commit to another branch. 

Why cherry-pick?

Because there are times when you can't merge all the commits in one branch to another due to various reasons. In those cases you can cherry-pick that single commit to intended branch without merging all the commits.

How to perform cherry-pick command





 

No comments:

Post a Comment