Github

Git Version Control Tutorial

What is GIT?

Git is a Version Control System that helps programmers keep track of the code they write, and also help them code in collaboration with others.

Installation:

You can install git from source code, or from your distro’s package management system. To install from the latest source, use the following commands:

$ wget -c https://git-core.googlecode.com/files/git-1.8.4.tar.gz
$ tar xvzf git-1.8.4.tar.gz
$ cd git-1.8.4
$ make install

Commit:

Commit is simply a snapshot of your working directory. Every time you commit your changes with a description, a commit object is created internally. It is a saving state that stores your changes. Therefore you can alter to your previous commit in case of any troubleshot.

The local repository:

In Git, you have a full copy of the repository locally. You can work offline without any connectivity to a master server and make commits to the local repository. To sync changes to the master server, you can add the remote repository and push changes into it, or sync your local repository with new changes in the remote repository.

The working directory and staging area:

The working directory is the repository directory where files are checked out. Whenever you want to do a commit after making some modifications, unlike other version control systems, git will not track the modifications made to all the files in the working directory. When you do a git commit, it wil look at the files added to the staging area. Only files in the staging are considerd for a commit.

Basic usage:

Setting up the identity:

Initially, after installing git, you need to set up the identity for the committer. You can either set a global identity for user-wide configuration, or create a repository-specific identity.

For example, to set a global user identity, use the following commands,

$ git config --global user.name "Ankan Biswas"
$ git config --global user.email ankan@example.com

NOTE: If you want to set a repository-specific private configuration, change the working directory to a git repository and invoke the above commands without –global option.

You can list all the git configuration:

$ git config list

or,

$ git config --global list

You can check the current user name and email:

$ git config user.name

and,

$ git config user.email

Creating an empty repository in the current directory:

$ git init

Adding files to the staging area:

$ git add file1 file2 file 3

NOTE: Every time you make a change to a file in the repository, you have to add that file to the staging area before making a commit.

To add the files it is also possible to select code chunks to be added from a file using the -p(prompt) option:

$ git add -p filename

(You select the code chunk using the y/n prompt)

Committing changes from the staging area:

$ git commit -m "message"

or,

$ git commit -a -m "message"

NOTE: -a denotes add. All modified files in the current directory are moved to the staging area and added to the commit.

Looking at the Git log:

Commits logs are very important for version control systems. To view the commit logs,

$ git log

show last two commits,

$ git log -2

you can also use the following,

$ git log -p

(-p stands for pretty. It also displays the difference between the last commits)

$ git log --since 'December 18 2013'
$ git log --before "2 days"
$ git log --after "3 days"

Status of the repository:

The git status command is very important. It is used to display the current status of the repository, and will show the contents of the working directory.

$ git status

Branches:

In git’s workflow, you can check which branch you are currently working with.

$ git branch

To create a new branch,

$ git branch branch_name

To switch between directory use:

$ git checkout branch_name

Shortcut to create and switch to a new directory:

$ git checkout -b branch_name

Checking out:

If you want to get files for a specified commit or branch, you can use,

$ git checkout branch_name

you can also check out only a single file from a branch with,

$ git checkout master filename

Cloning the a remote repository:

$ git clone repo_path

Here ,repo_path can be an HTTP URL, local UNIX-style path or SSH location.

Pushing local commits to a remote repository:

$ git push remote_repo branch_name

if you have created your local repository by cloning the main repository, the remote_repo name will be the origin. You can also push the changes with,

$ git push origin master

(pushed to master branch)

Hope this is useful. Thank you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s