Getting files in and out of the repository

There are two ways of getting source into the repository: adding new files or importing an existing directory tree.

Let's look at creating new files first.

Since we're going to be working with a new repository, you have to first create the local working copy. But there's nothing there, is there?

You can check out the CVSROOT directory mentioned in the previous section, as you can any other directory. Since that's all you have, you'll have to start with that. You also need to make a place for the local copy, which is called your sandbox (because we all like playing in sandboxes, right?) and put it in your home directory.:

cd $HOME
mkdir sandbox
cd sandbox

Now we need to get our working copy of the repository:

cvs -d$HOME/cvs checkout .

or:

cvs -d$HOME/cvs get .

The dot (.) for the filename translates to "give me the entire repository."

You'll notice a directory called CVS in every directory that you've checked out. CVS uses this directory to store information about where in the repository the files belong, the versions of the files, and so on. Don't change any of the information in this directory.

CAUTION:
If you create a new project by copying directories from one part of your sandbox to another, don't copy the CVS directory. If you do, your project probably won't get stored where you expect in the repository.

Now that we have a working copy, we can create some directories and files to demonstrate how to check in and out. We'll start with the standard "Hello, world" C program. It's good practice to keep all of your projects in separate directory structures, so we'll also create a new directory for our project.

To make the project directory:

mkdir myproj

Now we have to add this directory to the CVS repository:

cvs -d$HOME/cvs add myproj

It's time to create our test file. Make sure you're in the project directory:

cd myproj

Now use your favorite editor to create a file called foo.c with the following contents:

#include <stdio.h>

int main (int argc, char *argv[]) {
    printf ("Hello, world.\n");
}

Adding the file is very similar to adding the directory:

cvs add foo.c

Notice that we left out the -d option to cvs. This is intentional. When you check out a directory, CVS creates a directory of its own for status and administrative files, so that it knows which repository this directory was checked out from. All future operations apply to this repository.

This command tells CVS that you want to add the file. It isn't really added yet; CVS needs you to explicitly tell it when you've finished making changes to your local copy of the repository. This lets you change or add several files or directories in your own time, and then tell CVS to take the changes all at once when they're ready. You use the commit command to do this.