Thursday, 28 August 2014

Git useful commands

In bioinformatics GitHub and its protocol git is getting more and more popular. Here you will find an introduction about git and also some useful commands when you use Git in your projects. 

What is Git

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.
Useful commands

If you’re starting to track an existing project in Git, you need to go to the project’s directory and type:

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. 

If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add commands that specify the files you want to track, followed by a commit:

$ git add *.c
$ git add README
After added the files, you should be able to do the first commit:

$ git commit -m 'initial project version'

We’ll go over what these commands do in just a minute. At this point, you have a Git repository with tracked files and an initial commit.

Cloning an Existing Repository

If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to — the command you need is git clone. If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is clone and not checkout. This is an important distinction — Git receives a copy of nearly all data that the server has. Every version of every file for the history of the project is pulled down when you run git clone. In fact, if your server disk gets corrupted, you can use any of the clones on any client to set the server back to the state it was in when it was cloned.

You clone a repository with git clone [url]. For example, if you want to clone the Ruby Git library called Grit, you can do so like this:

$ git clone git://
That creates a directory named grit, initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. If you go into the new grit directory, you’ll see the project files in there, ready to be worked on or used. If you want to clone the repository into a directory named something other than grit, you can specify that as the next command-line option:

$ git clone git:// mygrit

That command does the same thing as the previous one, but the target directory is called mygrit.

Git has a number of different transfer protocols you can use. The previous example uses the git:// protocol, but you may also see http(s):// or user@server:/path.git, which uses the SSH transfer protocol.

Other useful commands:

#branches from currently checked out directory

$ git checkout -b <branchName>
Checkout a Remote Branch

$ git checkout -b <localBranchName> origin/<remoteBranchName>

Abort Changes of a File

$ git checkout -- <fileName>
Modify the Previous Commit's Message

$ git commit --amend
Partial Change Checkin

$ git add --edit

Undo the Previous Commit

$ git revert HEAD^
Temporarily Stash Changes, Restore Later

# After changes have been made...
$ git stash
# Do some other stuff here, like switch branches, merge other changes, etc.
#Re-apply the changes

$ git stash pop
Delete a Remote Branch

$ git push origin :<branchName>

Pull in the Latest from a Shared Repository

# Add a remote branch
$ git remote add <remoteName> <gitAddress>
# For example: git remote add lightfaceOfficial git://

# Get changes from that branch
$ git fetch <remoteName>

Tagging, Deleting, and Pushing Tags

# Create a Tag
$ git tag <tagName>

# Delete the tag
$ git tag -d <tagName>

# Push Tags
$ git push --tags