Your First Git Repositories

27 Jan

Creating a New Git Repository

Open up a terminal and cd into a directory where your project will live (or where an existing, non version-controlled project lives).

Type:

$ git init

You’ve now got a new repo.

gitignore

For new projects, you’ll probably want to create a .gitignore file to tell Git which files that you never want versioned. So open up a text editor, make a new file named .gitignore.

If you are building a Rails project you probably want to add things like:

log/*.log
tmp/**/*
doc/api
doc/app

If you are building a Java application using Eclipse and Maven, you might want something like:

.classpath
.project
.settings/
target/

There’s probably more that you’ll need to add to your project files, but that’s the idea.

For more on gitignore, see http://www.kernel.org/pub/software/scm/git/docs/gitignore.html

Cloning a Git Repository

The other (probably most common) way to get your hands on a repository, is to clone an existing one.

Something like:

$ git clone git://yourhost.com/path/to/repo-name.git

or

$ git clone https://hosthost.com/path/to/repo-name.git

or

$ git clone file:///local/path/to/repo-name.git

Adding Files and Committing Changes

Neither the act of creating a new repository nor cloning an existing repository adds any files to your changeset. You can edit files in your project, but the act of doing a commit will not commit any changes until you do a git add.

git status shows you the current status of your repo. If you haven’t modified any files, you’ll see something like:

$ git status
# On branch master
nothing to commit (working directory clean)

If you’ve modified files, but not added them to the changeset, you’ll see something like:

$ git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   CHANGED-FILE-NAME
#
no changes added to commit (use "git add" and/or "git commit -a")

Adding Files

To add all files (that aren’t excluded by .gitignore):

$ git add .

or to add a specific file


$ git add file-or-folder-name


git commit

commit the changes, and it’s always good to add a comment.

From Git Community Book:

A note on commit messages: Though not required, it’s a good idea to begin the commit message with a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. Tools that turn commits into email, for example, use the first line on the Subject: line and the rest of the commit message in the body

Common Way to Add Files As You Commit

git commit -a
Adds all changed files to stage before committing.

That’s good for today …

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Tumblr

No comments yet

Leave a Reply

Security Code: