Start using Git fast

May 23rd, 2009 2 Comments

Git branching and merging

In my previous post I said I would do a Git tutorial. It’s been a little while, but here it is. This will be a very brief guide, intended to get you started straight away. So, onwards!

The basics

git config

First of all you should set up your Git installation with your name and email:

git config --global user.name "Your name"
git config --global user.email "name@example.com"

git init

In any directory, with or without files, simply type git init to start a repository there. No other set up needed.

git status

When you have some files in the directory type git status. This will show you all the files that have changed and are untracked. (In any version control system you need to ‘track’ files you want to keep versions of.)

.gitignore

You may see some filename~ files. You most likely don’t want to track these, so type nano .gitignore, and enter ‘*~’ (without quotes). Press Ctrl+O to save, and Ctrl+X to exit. The *~ wild card will match any files ending in ‘~’, and cause Git to ignore them.

git add

Use git add filename.txt to start tracking your files. For example, you will want to add the .gitignore file from above, so type git add .gitignore. You will generally want to track all your source code files but not compiled ones, such as .class for Java. In fact, you can add *.class to your .gitignore file to make sure they don’t get added.

git commit

Finally, you can type git commit -m "Commit message here" to commit your new files.

The big difference

Unlike other version control systems, with Git you have to add files you want to commit to the ’staging area’ before committing. This means that each time you want to commit some changes, your work flow will be something like this:

$ git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#
#       modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git add test.txt
$ git commit -m "Made a change"
Created commit 2efe3ce: Made a change
 1 files changed, 1 insertions(+), 0 deletions(-)

Alternatively, you can use git commit -a -m "Commit message here" to commit all of your tracked files.

Basic conclusion

That’s all you really need to know to get started! One final recommendation is to install Meld, a fantastic visual diff program, which will show you all the changes you’ve made since your last commit. To install type sudo apt-get install meld. And then in your Git repository type meld . (note the trailing dot meaning ‘use this folder’).

There is of course much more to Git, such as branches and pushing to remote servers, but I’ll finish this introduction here and, depending on the response and persuasion I get, possibly write a further guide in the future.


2 Comments

Comments

2 Responses to “Start using Git fast”

  1. Matt Says:

    Cheers Stuart.

    I guess what I’d be interested in knowing about is the workflow you use with respect to remote repositories branching etc.

    I seem to be heading into a very simple

    git commit -a
    git push origin master

    with almost nil branching to basicly use a remote repo as a backup.

    Thanks. Matt.

  2. Stuk Says:

    Thought you might!
    I’ll do a follow up at some point. Spent longer than I expected on just this intro, so I left it as it was, ready for more :) . Although I pretty much just do what you do as well, commit/push

Leave a Reply