Sunday, January 26, 2014

GLANCE @ GIT


Create a new repository

Create a new directory, open it and perform a "$ git init" to create a new git repository.
eg:
$ mkdir example.git
$ cd example.git
$ git init [--bare]

Checkout a repository

Create a working copy of a local repository by running the command
"$ git clone /path/to/repository
when using a remote server, your command will be
"$ git clone username@host:/path/to/repository".

Workflow

Your local repository consists of three "trees" maintained by git. the first one is your Working Directory which holds the actual files. the second one is the Index which acts as a staging area and finally the HEAD which points to the last commit you've made.






Add & Commit 

You can propose changes (add it to the Index) using
"$ git add <filename>"
"$ git add *"
This is the first step in the basic git workflow. To actually commit these changes use
"$ git commit -a -m "Commit message"
Committing your changes "$ git commit"
Committing files from working directory skipping staging area " $ git commit -a"
Viewing commit history: "$ git log"

Now the file is committed to the HEAD, but not in your remote repository yet.

Pushing Changes

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute:
"$ git push origin master"
Change master to whatever branch you want to push your changes to.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
"$ git remote add origin <server>"
Now you are able to push your changes to the selected remote server

Forking

Forking and updating a repo:

  1. Fork this repository: This is a GitHub operation, in which you are making a copy of Joe's repository (including the files, commit history, issues, and more). This repository now lives in your GitHub account. Nothing has yet happened to your local computer.
  2. Clone your repo: This is a Git operation, in which you are using Git to tell GitHub "please send me a copy of my repo" The repo is now stored on your local computer.
  3. Update some files: You can now make updates to the files in whatever program or environment you like.
  4. Commit your changes: This is a Git operation, in which you are using Git to tell GitHub "here are my changes" Pushing does not happen automatically, so until you do this step, GitHub does not know about your commits.
  5. Push your changes to your github repo: This is a Git operation, in which you are using Git to tell GitHub "here are my changes" Pushing does not happen automatically, so until you do this step, GitHub does not know about your commits.
  6. Send a pull request to Joe: If you think that Joe might like to incorporate your changes, you send him a pull request. This is a GitHub operation, in which you are communicating your changes to Joe, and "requesting" that he "pull" form your repo. It is up to him whether he pulls from you or not.

Syncing a fork

Let's say that Joe and other contributors have made some more updates to the game, and you've thought of some more updates you'd like to make. Before you do anything else, it's best to "sync your fork" so that you are working on the latest copy of the files. Here's what you do:
  1. Fetch changes from Joe's repo: This is a Git operation, in which you are using Git to tell GitHub that you would like to retrieve the latest files from Joe's repo.
  2. Merge those changes into your repo: This is a Git operation, in which you are updating the repo on your local computer with those changes (which have been temporarily stored in a "branch"). Note: Steps 1 and 2 are often combined into a single Git operation called a "pull".
  3. Push the updates to your GitHub repo:(optional): Remember that your local computer does not automatically update your GitHub repo. Therefore, the only way to get your GitHub repo up-to-date is by pushing up the latest changes. You can either do this right away, or you can wait until you have made some updates of your own and committed them locally.
Take note of the contrast between the workflow for forking and the workflow for syncing: When you initially fork a repo, the flow of information is from Joe's repo to your repo, and then down to your local computer. But after that initial process, the flow of information is from Joe's repo to your local computer, and then up to your repo.

Branching

Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
create a new branch named "feature_x" and switch to it using
"$ git checkout -b feature_x"
switch back to master
"$ git checkout master"
and delete the branch again
"$ git branch -d feature_x"
a branch is not available to others unless you push the branch to your remote repository
"$ git push origin <branch>"
Checking Status of the files: "$ git status "

Update & Merge




To update your local repository to the newest commit, execute
[fetch+merge]"$ git pull"
[fetch + rebase] "$git pull --rebase"
in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
"$ git merge <branch>"
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After
changing, you need to mark them as merged with
"$ git add <filename>"
before merging changes, you can also preview them by using
"$ git diff <source_branch> <target_branch>"

Tagging

it's recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag named 1.0.0 by executing
"$ git tag 1.0.0 1b2e1d63ff"
the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag. You can get the commit id with
"$ git log"
you can also use fewer characters of the commit id, it just has to be unique.

Replace local changes

In case you did something wrong (which for sure never happens ;) you can replace local changes using the command
"$ git checkout -- <filename>"
this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this
"$ git fetch origin"
"$ git reset --hard origin/master"

Useful Hints

gitk: built-in git GUI
Use colorful git output: "$ git config color.ui true"
Show log on just one line per commit: "$ git config format.pretty oneline"
Use interactive adding: "git add -i"

Exceptions:

#1: fatal: This operation must be run in a work tree [duplicate]
Explanation: You repository is bare, i.e. it does not have a working tree attached to it. You can clone it locally to create a working tree for it, or you could use one of several other options to tell Git where the working tree is, e.g. the --work-tree option for single commands, the GIT_WORK_TREE environment variable, or the core.worktree configuration option.
"$ git config core.bare false"

Quick Reference:

Cloning a remote repo (that you created or forked on GitHub)

  • git clone < your-repo-URL >: copies your remote repo to your local machine (in a subdirectory with the repo's name), and automatically creates an "origin" handle
  • git remote add upstream < forked-repo-URL >: adds an "upstream" handle for the repo you forked
  • git remote -v: shows the handles for your remotes
  • git remote show < handlename >: inspect a remote in detail

Tracking, committing, and pushing your changes

  • git add < name >: if untracked, start tracking a file or directory; if tracked and modified, stage it for committing
  • git reset HEAD < name >: unstage a changed file
  • git commit -m "message": commits everything that has been staged with a message
    • -a -m "message": automatically stages any modified files, then commits
    • --amend -m "new message": fixes the message from the last commit
  • git push origin master: pushes your commits to the master branch of the origin

Syncing your local repo with the upstream repo

  • git fetch upstream: fetch the upstream and store its master branch in "upstream/master"
  • git merge upstream/master: merge that branch into the working branch

Viewing the status of your files

  • git status: check which files have been modified and/or staged since the last commit
  • git diff: shows the diff for files that are modified but not staged
    • --staged: shows the diff for files that are staged but not committed

Viewing the commit history

  • git log: shows the detailed commit history
    • -1: only shows the last 1 commit
    • -p: shows the line diff for each commit
    • -p --word-diff: shows the word diff for each commit
    • --stat: shows stats instead of diff details
    • --name-status: shows a simpler version of stat
    • --oneline: just shows commit comments
  • gitk: open a visual commit browser

Managing branches

  • git branch: shows a list of local branches
    • < branchname >: create a new branch with that name
    • -d < branchname >: delete a branch
    • -v: show the last commit on each local branch
    • -a: show local and remote branches
    • -va: show the last commit on each local and remote branch
    • --merged: list which branches are already merged into the working branch (safe to delete)
    • --no-merged: list which branches are not merged into the working branch
  • git checkout < branchname >: switch the HEAD pointer to a different branch
    • -b < branchname >: create a new branch and switch to it

Removing, deleting, and reverting files

  • git rm < name >: deletes that file from the disk, then stages its deletion
    • --cached < name >: stops tracking a file, then stages its deletion (but does not delete it from the disk)
  • git mv < oldname > < newname >: renames the file on disk, then stages the deletion of the old name and addition of the new name
  • git checkout -- < name >: revert a modified file on disk back to the last committed version

Other basic commands

  • git init: initialize Git in an existing directory
  • git config --list: shows your Git configuration
  • touch .gitignore: create an empty .gitignore file

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - -

$ git log -5 —oneline # check last 5 changes to repository

https://github.com

Username:
First you need to tell git your name, so that it can properly label the commits you make.
$ git config --global user.name “your name here”
$ Sets the default name for git to use when you commit.

Email: Git saves your email into the commits you make. We use the email address to associate your commits with your github account.
$ git config --global user.email “your_email@example.com”
$ sets the default email for git to use when you commit.



$ git clone <url_from_github>
# give password when required

# no need to run init because it already been initialized.

$ git remote -v # check git remote location
$ git remote add origin <url_from_github>

$ git status # to check status of new and modified file

$ git add . # add everything in current directory.

$ git commit -m “Committing file”

$ git status # to confirm nothing else to commit.
$ git log # to check everything is committed

$ git push origin master # push master to origin (remote) - give password when required.

Sync Fork with upstream require 2 operation:
  1. Fetch:  from remote to local
  2. Merge: merge changes and differences in local with others changes.
  3. Push: once satisfied push changes to remote repo. 

$ git remote -v
$ git remote add upstream <github url>
$git remote -v
$ git fetch upstream
- - if required we can switch upstream and view changes ---
$ git merge upstream/master
$ git push origin master # give password if required

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - -

Source Tree- GIT Client:

With recent update of Source Tree v 1.9.8 we start seeing problem where it fail to clone with message "warning: templates not found /usr/local/git/share/git-core/templates" on mac osX.

Following is the solution for this problem:
  1. Got to your home directory. $ cd ~
  2. open .gitconfig in vi (or your comfortable editor).
  3. Add this at beginning of file:
    1. [init]
      templatedir = /Applications/SourceTree.app/Contents/Resources/git_local/share/git-core/templates
This will resolve the issue, if required restart your Source Tree or machine.

GIT Hub /or GIT Lab Setting up SSH Keys

SSH key allows you to establish a secure connection between your computer and GitLab. Before generating an SSH key, check if your system already has one by running cat ~/.ssh/id_rsa.pub If your see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.

To generate a new SSH key just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename you can press enter to use the default. It is a best practice to use a password for an SSH key but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can't be altered or retrieved.

$ ssh-keygen -t rsa -C "reene.soan@gmail.com"

Use the code below to show your public key.
cat ~/.ssh/id_rsa.pub
------------------------
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDncSVBPs0hg5YRZ0NYJHueGpouluJPeKDpHO7gRsFSwUfkjGL5fKg4BCgxJZjdrHQ8G0e9jmt0oXYJpilde5olbrnx5NSEbP1hv18Mq8jTYEd91VH2HsrYJ2TNTgQ79JXA6m1v2UYTd15UwPkhzJEcpP29Cuh/MSX1URBA/aQ3wEfIc6LF45Kyx7St/kiBfPQwJfmcyRsIDv6ly6TQp8GT2nNzWVsFe6JigHN6XKf2YzFXK8oBGJZhv5vBNXYEr86rg7t34E8xYI/yBSGg1k2V+nymEfwhSRbmRyB+zdcvCVKVTSsgg3ySXj89EgDGULc7mfucNnkq0lThLVL1ObyJ reene.soan@gmail.com
------------------------


Copy-paste the key to the 'My SSH Keys' section under the 'SSH' tab in your user profile. Please copy the complete key starting with ssh- and ending with your username and host.

References:

GIT SCM, About, GitRef, Interactive Git Reference, Git 30 mins Crash Course

Sunday, January 12, 2014

How To Manually Install Oracle Java on Linux

Introduction

Java is a programming technology originally developed by Sun Microsystems and later acquired by Oracle. Oracle Java is a proprietary implementation for Java that is free to download and use for commercial use, but not to redistribute, therefore it is not included in a officially maintained repository.
There are many reasons why you would want to install Oracle Java over OpenJDK. In this tutorial, we will not discuss the differences between the above mentioned implementations.

Assumptions

This tutorial assumes that you have an account with DigitalOcean, as well as a Droplet running Debian 7 or Ubuntu 12.04 or above. You will need root privileges (via sudo) to complete the tutorial.
You will need to know whether you are running a 32 bit or a 64 bit OS:
$ uname -m
  • x86_64: 64 bit kernel
  • i686: 32 bit kernel

Downloading Oracle Java JDK

Using your web browser, go to the Oracle Java SE (Standard Edition) website and decide which version you want to install:
  • JDK: Java Development Kit. Includes a complete JRE plus tools for developing, debugging, and monitoring Java applications.
  • Server JRE: Java Runtime Environment. For deploying Java applications on servers. Includes tools for JVM monitoring and tools commonly required for server applications.
In this tutorial we will be installing the JDK Java SE Development Kit 8 x64 bits. Accept the license and copy the download link into your clipboard. Remember to choose the right tar.gz (64 or 32 bits). Use wget to download the archive into your server:
    $ wget --header "Cookie: oraclelicense=accept-securebackup-cookie" \
       http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz \
        --no-check-certificate 
 
Oracle does not allow downloads without accepting their license, therefore we needed to modify the header of our request. Alternatively, you can just download the compressed file using your browser and manually upload it using a SFTP/FTP client.
Always get the latest version from Oracle's website and modify the commands from this tutorial accordingly to your downloaded file.

Installing Oracle JDK

In this section, you will need sudo privileges:
    sudo su
The /opt directory is reserved for all the software and add-on packages that are not part of the default installation. Create a directory for your JDK installation:
    mkdir /opt/jdk
and extract java into the /opt/jdk directory:
    tar -zxf jdk-8u5-linux-x64.tar.gz -C /opt/jdk
Verify that the file has been extracted into the /opt/jdk directory.
    ls /opt/jdk

Setting Oracle JDK as the default JVM

In our case, the java executable is located under /opt/jdk/jdk1.8.0_05/bin/java . To set it as the default JVM in your machine run:
    update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_05/bin/java 100
and
    update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_05/bin/javac 100

Verify your installation

Verify that java has been successfully configured by running:
    update-alternatives --display java
and
    update-alternatives --display javac
The output should look like this:
    java - auto mode
link currently points to /opt/jdk/jdk1.8.0_05/bin/java
/opt/jdk/jdk1.8.0_05/bin/java - priority 100
Current 'best' version is '/opt/jdk/jdk1.8.0_05/bin/java'.

javac - auto mode
link currently points to /opt/jdk/jdk1.8.0_05/bin/javac
/opt/jdk/jdk1.8.0_05/bin/javac - priority 100
Current 'best' version is '/opt/jdk/jdk1.8.0_05/bin/javac'.
Another easy way to check your installation is:
    java -version
The output should look like this:
    java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

(Optional) Updating Java

To update Java, simply download an updated version from Oracle's website and extract it under the /opt/jdk directory, then set it up as the default JVM with a higher priority number (in this case 110):
    update-alternatives --install /usr/bin/java java /opt/jdk/jdk.new.version/bin/java 110
update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk.new.version/bin/javac 110
You can keep the old version or delete it:
    update-alternatives --remove java /opt/jdk/jdk.old.version/bin/java
update-alternatives --remove javac /opt/jdk/jdk.old.version/bin/javac

rm -rf /opt/jdk/jdk.old.version