Get Up and Running with Github

Github is a code sharing and publishing services, or in another words, it is social networking site for programmers. Github is used to store the projects and every versions of it. It uses the VCS (Version Control System) to store the projects to avoid loss of data and availability. Although it's mostly used for storing codes, it could be used by any other type of file such as Word documents or Final cut projects. Github was created in 2005 by Linus Torvalds (the creator of Linux) due to failure of BitKeeper. Since its birth in 2005, Git has evolved and most popular for its branching system for  non-linear development.
About Version Control (VCS)
Version control is the system that keeps the records of every changes and version of particular file, so you can recall the specific version later. When you are working on projects, VCS is very wise thing to use. It gives you the ability to revert files back to its previous states or original state when something happens that might causes the problem. It is very useful in case of data loss and corruption.
Centralized Version control System (CVCS)   
In real workplace, we often need to collaborate with other developers on other systems. To handle with that problem, Centralized Version Control (CVCSs) were developed. This system have central single server which would store all the versioned files, and clients would check out the files from central place. It offers many advantages, but it has some serious disadvantages. Since it is centralized system, if the server goes down for an hour, then no one can collaborate at all or access the files from the server. Morever, If database get corrupted, then there is risk of loss of everything. This led to the invention of new VCS.
Distributed Version control System (DVCS)
In DVCS, client can check the files and fully mirror the repository. Thus if server dies, client repositories can be used to restore data which prevent us from data loss. Every clone is a full backup of all data. It deals well with remote repositories, thus developer can collaborate with different groups of people in many ways to work simultaneously on single project. This system has some workflows which can't be possible to handle such as hierarchical models.
Difference between Git and other VCS
The major difference between Git and other VCS is the way Git thinks about its data. Git thinks of its data as set of snapshots of a miniature filesystem. Everytime you commit, it saves the picture of what your all files look at the moment and stores the reference to that snapshot. Git thinks about its data more like    stream of snapshots     . Git has entire history of your project on your local disk , therefore no need to contact the server in order to get the previous version file. This makes access to your history of files instantly.


Another powerful feature of Git is integrity. Every data is check-summed by Git before storing it which would make it impossible to change file or directory without Git knowing about it. The mechanism that Git uses for this checksumming is known as SHA-1 hash. This is a 40 character string composed of hexadecimal character and calculated based on the contents of a file or directory structure in Git.


Git has main three states that files goes through   committed, modified, and staged.     
  1. Committed : data stored safely to your local database.
  2. Modified : you have changed the file but have not committed it to your database.
  3. Staged : means that you have marked the modified file in current version to get into next commit snapshot.


To get the Git shell click here : Download >>Github Command Line<<
In this article, we will learn about command line.

Identity
The first step after installing Git is to set your username and email address. This is important because every Git commit uses this credentials to store and retrieve the files. You can achieve that as follows
$ git config --global user.name "John Doe"

$ git config --global user.email johndoe@example.com
// --global : it useful when you want to specify Git to use the same information for anything you do on that system.

$ git config --global core.editor emacs
// To configure default text editor. If not configured, Git would use system's default editor, generally Vim.

    If you want to check your settings stored in local system on shell, you can use git config --list, 

$ git config --list
user.name=John Doeuser.email=johndoe@example.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto...
You can also check specific details by doing following
  
$ git config user.emailexample@hotmail.com
$git config user.nameJohn Doe
$git help // for help $git --help config //show help about particular word if valid
Initialize a Repository in an Existing Directory Or Create New one 
 Suppose, you want to create a new directory and initialize the git repository, then you can do as follows

$ mkdir c:/mygitrepository
//Creates a new directory on your disk
$ cd c:/mygitrepository
// tells your shell to go on specified directory
$ git init
// Initialize the repository by creating .git hidden file in the directory
//If you want to initialize repository in current directory (Whichever default loaded in shell) , then simply type git init . This creates a new subdirectory named .git that contains all of your necessary repository files known as Git repository skeleton. If you have files in that directory and you want to start version controlling then, you can accomplish it with few commands

$ git add *.c
// *.c would select all files with .c extension. Assuming it is C language project files.
$ git commit -m "first version of file"
//add any message which helps you when you want to restore file.
//commit would track files in your local repository

 Cloning Existing Repository  
if you want to copy existing Git repository, then you can use command git clone. This would fetch fully copy of nearly  all data that server has with all versions. Every version of every file of project would be pulled down by default when you run git clone. For cloning repository, you would need repository URL which would be provided by server user interface.

For example, you can do that as follows

 
// fetching data from Git repository to your local repository.
$ git clone https://github.com/repositoryname/cproject  
// this would copy all of your server project files on your current directory on shell you are working on
Cloning into 'cproject'...
remote: Counting objects: 94, done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 94 (delta 7), reused 91 (delta 7), pack-reused 0
Unpacking objects: 100% (94/94), done.
Checking connectivity... done.

//if you want to copy repository in specific directory specify directory after the Url
$ git clone https://github.com/repositoryname/cproject c:/mygitcopy/copyhere

// Git has different transfer protocols which you can use. Url might differs depending on the protocol you use for transfer.
// https:// protocol, git:// , or ssh protocol - user@server:path/to/repo.git

Checking the status of your files, to determine the status of file whether its tracked or untracked, you can use git status command. If you would run this command, after cloning repository, you would not see any tracked or modified files, because the repository is just cloned, you need to commit AND add those files in your local repository to mark it as tracked file.

 Remember : Whenever you modify or create a new file in your repository/directory those files needs to be tracked in your local repository (.git folder on your local drive). When you track those files in your local repository then you would be able to send those files to server repository. If you want to check the status of your files then use   git status     command. It will tell you how many files are tracked and which are need to be tracked. You can track those files by command   git add <file>

Suppose, you have created a ReadMe file in your repository ( git init - to initialize repository in your current directory) , you can do as follows

 $ touch ReadMe   //create a file in your current directory
$ git status // It will check untracked files n will inform you to track new file in your repository
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: README.

/////////////////////////////////////////////

// suppose you had already had a file contributing.md file in your directory and was tracked before.
// if you change that contributing.md and if you check status
$ git status // will inform you to track modified file in your repository
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: README
modified: CONTRIBUTING.md

Git is quite wordy and easy to use. If you want to see short status, you can use git status --short / git status -s. 


$ git status -s
M README
MM Rakefile
A lib/git.rb //A - files added to staging area
M lib/simplegit.rb // M - Modified file
?? LICENSE.txt // ?? - Untracked Files


if you want to know what exactly have you changed in file,and not just files were changed, you can git status.
 Viewing your Changes and Ignoring tracking of specific files 
if you want to know what exactly you changed in file, not just which files were changed, you can use git diff command.
If you want to see what you have staged that will go into your next commit, you can use git diff --staged command.
Use git diff --cached to see what you have staged so far. brief example as follows




$ git diff --staged
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+My Project

often, you don't want some of your files added to the repository or not even want to see it as tracked or untracked. Those files might be generated automatically by your system or temporary files. To exclude those from getting tracked, you can customized .gitignore file. This file is created in your repository at the time of initialization specifically for excluding specific file purpose. This file contains the wild card patterns which would tell the Git repository what needs to be excluded from being tracked. for example, below is the


$ cat .gitignore   //cat command to read files/open files - vi command to open in editor 
*.[oa] // wild card pattern - tells Git to ignore any files ending in ".o" or ".a" (extension of file)
*~ // tells Git to ignore any file name ending with ~

# The rules for the patterns you can put in the .gitignore file are as follows:
#• Blank lines or lines starting with # are ignored.
#• Standard glob patterns work.
#• You can start patterns with a forward slash (/) to avoid recursivity.
#• You can end patterns with a forward slash (/) to specify a directory.
#• You can negate a pattern by starting it with an exclamation point (!).
you can always open .gitignore file and add the patterns of file or extension of file you want to avoid being tracked in your repository.
Commit Changes
 After you track the files, you are ready to commit those changes. You can commit your changes by using git commit command. When you will type this command, it will open your default editor (generally Emacs or Vim) to add your message for committing those changes. Messages are important and helps you understand why you made those changes. It act as hint to understand that you did this commit in order to accomplish certain task. You can also avoid opening editor and directly pass message while committing your data to repository. you can do it as follows

$ git commit -m "my first commit"           
// -m means message
$ git commit -a -m "my second commit"
// -a option to skip the staging area
$ git log //To view the commit history
$ git log -p -2
// -p option for showing the difference introduced in each commit and -2 limits the results will show only two results
$ git commit --amend //Undo your changes with last commit
after commit, you are ready to upload your code to server on remote site. The reason why commit is necessary is because, it saves local copy of your repository on your machine, so you can work even when the server is down, and you have your own backup of your repository. You can also work without having internet connection using your local repository. This is why commit command is important and last step to have your data packed in your repository.
Working with Remotes - Important part of Github 
Remote repositories are the files of final version of project and hosted on the internet. It is very necessary to have knowledge of handling remote repositories to collaborate on Git project. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from when you need work together on shared work.

you can see the details of your origin ( web remote address) using following commands :
$ git remote -v   
// -v to show URL of Git storage
origin git://github.com/schacon/ticgit.git
//If you have added more than once repositories - you can access all the contributions very easily from different repositories..
$ git remote -v
bakkdoor git://github.com/bakkdoor/grit.git
cho45 git://github.com/cho45/grit.git
defunkt git://github.com/defunkt/grit.git
koke git://github.com/koke/grit.git
origin git@github.com:mojombo/grit.git
Adding Remote Repository 
You can add remote repositories remotely. To add new Git repository as a your desired name, so you can actually reference your repository and access it without remembering actual URL of the repository. you can do it using git remote add [yourDesiredName] [url].

$ git remote
origin

$ git remote add pb git://github.com/paulboone/ticgit.git
// added repository as pb reference
$ git remote -v
origin git://github.com/schacon/ticgit.git
pb git://github.com/paulboone/ticgit.git
//showed as added repositories
//now you can use string pb on command line so that you don't need to remember URL everytime.

//If you want to fetch all the data that pb repositories has then simply type
$git fetch pb
Fetching and Pulling data from your Remote Repository
To get data from your remote projects, you can run : git fetch
This command pulls down all the data from your remote project that you don't have yet. after you do this, you should have references to all the branches from the remote, which you can merge or inspect at any time.
$ git fetch [remote-name]
Difference between Fetch and Clone : If you clone a repository, the command automatically adds that remote repository under the name  origin  . So,   git fetch origin    fetches any new work that has been pushed to that server since you cloned (or last fetched from) it.

Important to note that the fetch command pulls the data to your local repository
— it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.

 you can use the git pull command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default,the git clone command automatically sets up your local master branch to track the remote master branch on the server you cloned from (assuming the remote has a master branch).    Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently working on.  
Pushing date to your Remote repository 
When your project is complete and if you want to share, then you have to push it to remote. The command for this is simple    git push [remote-name] [branch-name].
If you want to push your master branch to your origin server (again, cloning generally sets up both of those names for you automatically), then you can run this to push your work back up to the server:

$git push origin master 
This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to pull down their work first and incorporate it into yours before you’ll be allowed to push.
Inspecting a Remote repository
If you want to see detailed information about specific remote, then you can use    git remote show [remote-name]  command. if you run this command with particular reference name of remote, you will get something like this

$ git remote show origin
* remote origin
URL: git://github.com/schacon/ticgit.git
Remote branch merged with 'git pull' while on branch master
master
Tracked remote branches
master
ticgit
Removing and Renaming a Remote Repository
If you want to rename or remove a reference to remote repository, you can do it as follows
$ git remote rename pb paul   //changing the name from pb to paul
$ git remote
origin
paul

//To remove a reference you can do it as follows

$ git remote rm paul

$ git remote


origin


Getting started when you create repository
When you create a new repository,
you can do minimal necessary steps in order to upload or fetch data, you can do by following commands,


You just have to replace url with your Origin URL and you are good to go. Have fun using Git.
Summary
 At this point, you can do all the basic local Git operations — creating or
cloning a repository, making changes, staging and committing those changes,and viewing the history of all the changes of the repository.  

References : Chacon, Scott, and Ben Straub. Pro Git. 2nd ed. N.p.: Apress, n.d. PDF.


Post a Comment

0 Comments