Git

by Dave on May 4th, 2010

Git is a version control system which allows you to track changes to projects code amongst many other things.

First-time system setup

Do these steps once per computer.

Head over to Installing Git section of Pro Git and install Git.

$ git config --global user.name "Your Name"
$ git config --global user.email youremail@example.com
$ git config --global core.editor "mate -w"

(this last line is if you’re using textmate)

Setting up a NEW Repository for your App

Generally you will setup a repository for each application you create.

1. Initalize the repository

$ git init
Initialized empty Git repository in /Users/username/rails_apps/first_app/.git/

2. Add the project files to the repository (but ignore some first!)

By default Git tracks the changes of all files, but there are some we don’t want to track. For example, the log files. We ignore files by including a file named .gitignore in the Rails root directory. Generate this file using your text editor and here’s a sample of what you might put in it.

log/*.log
tmp/*
tmp/**/*
doc/api
doc/app
db/*.sqlite3
*.swp
*~
.DS_STORE

Now we may add files to Git.
$ git add .

The dot ‘.’ represents the current directory and Git is smart enough to add the files recursively so it automatically adds all subdirectories. This places the projects files in a staging area. You can see what files are in the staging area using the status command.
$ git status

3. Committing files to the repository

To tell Git you want to keep the changes in the staging area, use the commit command.
$ git commit -m "Initial commit"

The -m lets you add a message for the commit.

Remember, Git commits are local, recorded only on the machine on which the commits occur.

To push the changes to a remote repository you must use git push.

Branch, Edit, Commit, Merge

When making changes to an application use this process.

1. Checkout a Branch


$ git checkout master
$ git checkout -b 'branch name'

Notice the -b flag for checking out a branch. The first line just ensures we are branching from the master.

git branch will list all the local branches, with an asterix identifying which branch we’re currently on.

2. Edit

Edit your files as required.

3. Commit

If you’ve added new files make sure you use git add . prior to doing a commit. Otherwise you may use the -a flag with your commit if you’ve modified existing files.
$ git commit -a -m "comment goes here"

4. Merge

When finished making changes, you merge these back to your master branch.
$ git checkout master
Switched to branch 'master'
$ git merge modify-README

Change modify-README to the name of your branch.
Afterwards you can tidy up your branches by deleting the topic branch.
$git branch -d modify-README

Push your changes to a remote repository

Use the push command
$ git push

From → General

No comments yet

Leave a Reply

You must be logged in to post a comment.