Git: Getting started

The basic idea

Each time a change is made to your project, a snapshot can be made of the current state. This is called a "commit". Every commit is linked to the previous version, making it possible to traverse back in time.

Commits link to older versions.

This makes it possible to undo changes anytime. Git also allows to share those changes with developers working on other parts of the software.

Making it all work

Git is developped as a set of command-line tools. On top of these tools, other developers have built nice graphical frontends. Feel free to use whatever tool suites you best:



Git clients for Windows:

Easy to use, with clear overview, and integration in the Visual Studio toolbar.
Nice integration with the Windows Explorer, yet offers less helpful dialogs compared to GitExtensions. msysGit should be installed manually.
A reasonable option for users of SmartSVN. Also features helpful dialogs. The interface however, is less adapted to Git's workflow because it does not favor a prominent log dialog.


Git clients for Linux:

A powerful gui for git.
A graphical frontend, built with GTK+.
A repository viewer, similar to GitX on Mac OS X.
A graphical frontend, build with Qt.


Git clients for Mac OS X:

A popular Git client for Mac OS X.
A promising Git client.

The command prompt

In this tutorial, the Git command prompt is used, but the concepts of the tutorial are easily applied to any graphical Git client. The command prompt is used for a few simple reasons:

Basic setup

First, You need to introduce yourself to git.

Tips of Windows users

In the "Git bash" command prompt, you can auto-complete commands and parameters with the TAB key. Press TAB again to see a list of suggestions.

Some interactive Git commands open vim as default editor, which you can exit with :q! <enter>. By changing the core.editor option, the editor can be changed to Notepad2 for example.

Enter these commands:
git config --global  "Diederik van der Boor"
git config --global "vdboor@...."

And optionally, configure some settings:

Useful settings:
git config --global  color.ui    auto     # colorize all messages
git config --global    status   # alias "git status" as "git st"
git config --global  core.editor kwrite   # e.g. you dislike VIM as editor

You can also edit these settings in the ~/.gitconfig file.

The basic commands

As quick reference, here are the basic commands to work with Git:

Importing the project

In the root directory of a project at your computer, type:

git init

Now, git is able to track files, and you can add them:

Adding all files:
git add .       # adds the whole project folder
git commit -a   # commits everything

Git opens your text editor where you can enter a commit message. Type the commit message, save the file, and quit.

To abort, just close the file without saving it.

Tips for finding help

A basic overview of the Git commands can be found with:
git --help

Every sub command has a --help option too, for example:
git config --help

You can quit the manual page by pressing q.

Viewing changes

There are a few commands to get information about your history:

Getting information:
git log             # show a log of changes
git status          # see which files you changed
git diff            # view uncommitted changes in the working copy
git show commitid    # show one commit

Undoing local changes

There are a few commands to undo local changes:

Undo local changes:
git checkout filename    # restore original, throw local changes away
git checkout -f         # throw all local changes away  (be careful!)
git reset --hard        # the same

That's all you need!

You can use Git for your projects at your computer now. Each time a new feature is complete, you can commit it with git commit -a. New files need to be added first with git add.

In the next chapter, you'll find more tips and insights to fully get up speed.

blog comments powered by Disqus