1.08.2011

Understanding Git - Part- II

What is Git?

Git is a distributed revision control system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development.

Every Git working directory is a full-fledged repository with complete history and full revision tracking capabilities, not dependent on network access or a central server.

Git’s current software maintenance is overseen by Junio Hamano. Distributed under the terms of version 2 of the GNU General Public License, git is free software.

Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

How Git Works?


Git is a decentralized version control system which means that there is no any kind of centralized repository (as SVN) rather we have distributed repository where each user has their own repository and they have freedom to create branches and experiment rather than creating branches at the central repo (as in SVN).

How to install Git on Windows and Linux?

Windows

1. Download Cygwin.
2. Put setup.exe in a folder of its own in your documents.
3. Launch setup.exe.
4. While installing Cygwin, pick these packages:
* git from the DEVEL category
* nano (if you’re wimpy) or vim (if you know it), both in the EDITORS category

You’ll now have a shortcut to launch Cygwin, which brings up something like the Linux terminal.

Linux

Install the git package using your preferred method (package manager or from source).

yum install git-core

Introduce Yourself to Git

Fire up your Cygwin/Linux terminal, and type:

git config --global user.name "Joey Joejoe"
git config --global user.email "joey@joejoe.com"

You only need to do this once.

Start Your Project

Start your project using the Sphere editor, or from a ZIP file, or just by making the directory and adding files yourself.

Now cd to your project directory:

cd myproject/

Tell git to start giving a damn about your project:

git init

… and your files in it:

git add .

Wrap it up:

git commit

Now type in a “commit message”: a reminder to yourself of what you’ve just done, like:

Initial commit.

Save it and quit (type Ctrl+o Ctrl+x if you’re in nano, :x if you’re in vim) and you’re done!

When dealing with git, it’s best to work in small bits. Rule of thumb: if you can’t summarise it in a sentence, you’ve gone too long without committing.

This section is your typical work cycle:

1. Work on your project.
2. Check which files you’ve changed:

git status

3. Check what the actual changes were:

git diff

4. Add any files/folders mentioned in step 2 (or new ones):

git add file1 newfile2 newfolder3

5. Commit your work:

git commit

6. Enter and save your commit message. If you want to back out, just quit the editor.

Repeat as much as you like. Just remember to always end with a commit.

What you did till now?

To see what you’ve done so far, type:

git log

To just see the last few commits you’ve made:

git log -n3

Replace 3 with whatever you feel like.

For a complete overview, type:

git log --stat --summary

Browse at your leisure.

What changes you made?

To view changes you haven’t committed yet:

git diff

If you want changes between versions of your project, first you’ll need to know the commit ID for the changes:

git log --pretty=oneline

6c93a1960072710c6677682a7816ba9e48b7528f Remove persist.clearScriptCache() function.
c6e7f6e685edbb414c676df259aab989b617b018 Make git ignore logs directory.
8fefbce334d30466e3bb8f24d11202a8f535301c Initial commit.

The 40 characters at the front of each line is the commit ID. You’ll also see them when you git commit. You can use it to show differences between commits.

To view the changes between the 1st and 2nd commits, type:

git diff 8fef..c6e7

Note how you didn’t have to type the whole thing, just the first few unique characters are enough.

To view the last changes you made:

git diff HEAD^..HEAD

Troubleshoot?

Haven’t committed yet, but don’t want to save the changes? You can throw them away:

git reset --hard

You can also do it for individual files, but it’s a bit different:

git checkout myfile.txt

Messed up the commit message? This will let you re-enter it:

git commit --amend

Forgot something in your last commit? That’s easy to fix.

git reset --soft HEAD^

Add that stuff you forgot:

git add forgot.txt these.txt

Then write over the last commit:

git commit

Don’t make a habit of overwriting/changing history if it’s a public repo you’re working with, though.

Hope you liked this post relevant and useful.

In the next article we shall dig deep into Workflow which makes Git flexible tool to use both the decentralized and centralized version control system.

Please go through this presentation before we proceed with the different workflow.

Git Vs SVN

Introduction to Git

No comments:

Post a Comment