Saturday, January 19, 2013

A Super Quick Start To Git

Git is a distributed version control system. This post is helpful to someone who would like to get quickly started with Git. Once up and running with the commands below, the reader is encouraged to follow-up with the comprehensive documentation on Git available from many excellent online sources. This will help to grasp the core concepts of Git, which are essential for regular use.

Note that this post only covers operations on a local repository. Distributed operations like cloning, pulling, pushing etc. will be covered in a future post.

Quick Git Concepts

  • Git is fully-functional and self-contained with just a local repository. Committing, branching, merging etc. can all be performed locally with no requirement for a central server.
  • Branching and merging are light-weight and extremely fast (local) operations.
  • The (hidden) .git directory within the root of working directory is fully self-contained. Git maintains the entire repository that includes information of all of the branches, commits etc. in this single directory.

Initial Setup

Git embeds the user name and e-mail address in every commit. This configuration is required before we can commit to the repository.

Create A New Repository

Initialises a new Git repository in the current directory. A single .git directory is created in the current directory where Git maintains the repository information.

Add Files To The Staging Area

Add specified files to the staging area. Wildcards can be used (e.g. *.java). Note that staged files need to be explicitly committed to the repository.

Adds any modified files previously committed and also any new files to the staging area.

Check The Current Status Of The Repository

Returns the current status of the repository. This is helpful to see the status of files that are in the staging area but not yet committed.

Commit Staged Files Into The Repository

Branching And Merging

Branching and merging are extremely light-weight operations in Git. In fact, it is even possible, if required to branch and merge several times a day. The root in Git is known as the Master branch. An important point to note is that only a single branch can be checked-out in the working directory at a given moment.

List All Of The Branches In The Repository

Create And Switch To A New Branch

Switch To An Existing Branch

Merging Branches

Merges the specified branch into the current branch.

Deleting A Branch

Note that you cannot delete the branch that is currently checked-out.