Monday, March 10, 2014

Lets start to use GITHUB !

Why use something like Git? Say you and a coworker are both updating pages on the same website. You make your changes, save them, and upload them back to the website. So far, so good. The problem comes when your coworker is working on the same page as you at the same time. One of you is about to have your work overwritten and erased.

A version control application like Git keeps that from happening. You and your coworker can each upload your revisions to the same page, and Git will save two copies. Later, you can merge your changes together without losing any work along the way. You can even revert to an earlier version at any time, because Git keeps a “snapshot” of every change ever made.

You have to Download GitHub at first.

Here is all common command of Github bash. So have a look before starting work. Don't need to afraid seeing lots of command !


CREATE

Clone and existing repository
$ git clone ssh://user@domain.com/repo.git

Create a new local repository
$ git init

LOCAL CHANGES

Changes files in your working directory
$ git status [Get used to this command after every action]

Add all current changes to the next  commit
$ git add .

Add some changes in <file> to the next commit
$ git add -p <file>

Commit all local changes in tracked files
$ git commit -a

Commit previously staged changes
$ git commit

COMMIT HISTORY

Show all commits, starting with newest
$ git log

Show changes over time for a specific file
$ git log -p <file>

Who changed what and when in <file>
$ git blame <file>

Branches & tags

List all existing branches
$ git branch

Switch HEAD branch
$ git checkout <branch>

Create a new branch based on y our current HEAD

$ git branch <new-branch>

Create a new tracking branch based on a remote branch
$ git branch --track <new-branch> <remote-branch>

Delete a local branch
$ git branch -d <branch>

Mark the current commit with a tag
$ git tag <tag-name>

Update & Publish

List all currently configured remotes
$ git remote -v

Show information about a remote
$ git remote show <remote>

Add new remote repository , named <remote>
$ git remote add <remote> <url>

Download all changes from <remote>, but  don‘t integrate into HEAD
$ git fetch <remote>

Download changes and directly merge/ integrate into HEAD
$ git pull <remote> <branch>

Publish local changes on a remote
$ git push <remote> <branch>

Delete a branch on the remote
$ git push <remote> :<branch>

Publish y our tags
$ git push --tags

Merge & Rebase

Merge <branch> into y our current HEAD
$ git merge <branch>

Rebase y our current HEAD onto <branch>
Don‘t rebase published commits!
$ git rebase <branch>

Abort a rebase
$ git rebase --abort

Continue a rebase after resolving conflicts
$ git rebase --continue

Use y our configured merge tool to solve conflicts
$ git mergetool

Use y our editor to manually solve conflicts and (after resolving) mark file as resolved

$ git add <resolved-file>

$ git rm <resolved-file>

Undo

Discard all local changes in y our working directory
$ git reset --hard HEAD
Discard local changes in a specific file
$ git checkout HEAD <file>
Revert a commit (by producing a new commit with contrary changes)
$ git revert <commit>

Reset y our HEAD pointer to a previous commit
…and discard all changes since then
$ git reset --hard <commit>

…and preserve all changes as unstaged
changes
$ git reset <commit>

…and preserve uncommitted local
changes
$ git reset --keep <commit>


How you will use GitHub bash :


After installing go to the directory where you want to keep the source control of your project. Press your mouse right option and press git bash. Write down " git init " . It will create git initialize for that folder.




Now write down " git status "you will see you are on the master branch.





















Now open a new project. After new project write down git status. As you have create a new project you will see that project untracked. You can add all the file using " git add . " . Now you can check the status using " git status ". You will see all files has been added.


















If its seems to you everything is okay for making one version for the project then write down " git commit ". You will get prompt for writing message for that specific commit. After completing message press Esc and write down :wq to go back to the command line.




You can show all the commits , starting with newest using " git log " .






















If you want to show changes over time for a specific file " git log -p <filename> " .
Press tab after -p you will get suggestion from cmd prompt for the folder or file. It will save your time to write in down the foldername and filename also it will minimize the error on writing.

IMPORTANT : To back to the bash command press Esc and write down :wq .




If you want to see who changed what file and when use git blame <filename> " .





















If you want to check all the branches use " git branch " . If you want to switch to head branch  " git checkout master " .






















Sometimes its necessary to show branch with details. To show details use " git show-branch --list " . If you need all commit with details with some excellent color theme you can use " git log --graph git log --graph --full-history --all --pretty=format:"%h%x09%d%x20%s" "

With colors (if your shell is Bash): " git log --graph --full-history --all --color \ --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"  "






















If you need to delete a local branch use " git branch -d <branch-name> " . You can mark the current commit with a tag. To tag the current commit you have to use " git tag <tag-name> " .


















Create a repository on your github account.

















Add a new remote repository using
" git remote add origin  https://github.com/SiddiqGoogleGlass/project_test.git "





















Now you can push your code to the server using " git push -u origin master " . Make sure you are in same branch name. As you are trying to push origin master so you have to checkout to master branch in local and push from that branch.


















Now you will see the updated code has been pushed to the github server.








You will get the HTTPS clone URL from here the github server. Now another developer of your team can work on this repository. To work on this repository at first need to clone the project. Your co-developer can clone the project using " git clone <repo-url> ".  

















After clone successful your team member will get success message. After that what you need to do first is " git status " . After that create one branch for local github like " local_development " using " git branch local_development ". Then checkout to that local_development using " git checkout local_development " . Now your team member can work on that branch to track his/her code.

If you or your team member need to get the older code that have cloned or coded or other branch code then just checkout to that branch and you will get the exact code of that branch in your source code.

Awesome !!! Now you and your team members can code at same time without any hassle. Happy coding!


Best practices you should follow : 
Commit related changes
Commit often
Don't commit half-done work
Test code before you commit
Write good commit message
Use branches
Agree on a workflow

Some exception you will get : 
1. Pushing to Git returning Error Code 403 fatal: HTTP request failed

http://stackoverflow.com/questions/7438313/pushing-to-git-returning-error-code-403-fatal-http-request-failed clear will clear the cmd prompt screen

Help & Documentation 

Git help on the command line
$git help <command>

Official git website
www.git-scm.com

Free online resources
www.progit.org
bookgit.git-scm.org
www.gitref.org