Go to the first, previous, next, last section, table of contents.

# Adding, removing, and renaming files and directories

In the course of a project, one will often add new files. Likewise with removing or renaming, or with directories. The general concept to keep in mind in all these cases is that instead of making an irreversible change you want CVS to record the fact that a change has taken place, just as with modifying an existing file. The exact mechanisms to do this in CVS vary depending on the situation.

- Adding files: Adding files
- Removing files: Removing files
- Removing directories: Removing directories
- Moving files: Moving and renaming files
- Moving directories: Moving and renaming directories

## Adding files to a directory

To add a new file to a directory, follow these steps.

- You must have a working copy of the directory. See section Getting the source.
- Create the new file inside your working copy of the directory.
- Use ``cvs add filename'` to tell CVS that you want to version control the file. If the file contains binary data, specify ``-kb'` (see section Handling binary files).
- Use ``cvs commit filename'` to actually check in the file into the repository. Other developers cannot see the file until you perform this step.

You can also use the `add` command to add a new directory.

Unlike most other commands, the `add` command is not recursive. You cannot even type ``cvs add foo/bar'`! Instead, you have to

    $ cd foo
    $ cvs add bar

Command: cvs add [`-k` kflag] [`-m` message] files ...

  Schedule files to be added to the repository. The files or directories specified with `add` must already exist in the current directory. To add a whole new directory hierarchy to the source repository (for example, files received from a third-party vendor), use the `import` command instead. See section import--Import sources into CVS, using vendor branches.

  The added files are not placed in the source repository until you use `commit` to make the change permanent. Doing an `add` on a file that was removed with the `remove` command will undo the effect of the `remove`, unless a `commit` command intervened. See section Removing files, for an example.

  The ``-k'` option specifies the default way that this file will be checked out; for more information see section Substitution modes.

  The ``-m'` option specifies a description for the file. This description appears in the history log (if it is enabled, see section The history file). It will also be saved in the version history inside the repository when the file is committed. The `log` command displays this description. The description can be changed using ``admin -t'`. See section admin--Administration. If you omit the ``-m description'` flag, an empty string will be used. You will not be prompted for a description.

For example, the following commands add the file ``backend.c'` to the repository:

    $ cvs add backend.c
    $ cvs commit -m "Early version. Not yet compilable." backend.c

When you add a file it is added only on the branch which you are working on (see section Branching and merging). You can later merge the additions to another branch if you want (see section Merging can add or remove files).

## Removing files

Modules change. New files are added, and old files disappear. Still, you want to be able to retrieve an exact copy of old releases.

Here is what you can do to remove a file, but remain able to retrieve old revisions:

- Make sure that you have not made any uncommitted modifications to the file. See section Viewing differences, for one way to do that. You can also use the `status` or `update` command. If you remove the file without committing your changes, you will of course not be able to retrieve the file as it was immediately before you deleted it.
- Remove the file from your working copy of the directory. You can for instance use `rm`.
- Use ``cvs remove filename'` to tell CVS that you really want to delete the file.
- Use ``cvs commit filename'` to actually perform the removal of the file from the repository.

When you commit the removal of the file, CVS records the fact that the file no longer exists. It is possible for a file to exist on only some branches and not on others, or to re-add another file with the same name later. CVS will correctly create or not create the file, based on the ``-r'` and ``-D'` options specified to `checkout` or `update`.

Command: cvs remove [options] files ...

  Schedule file(s) to be removed from the repository (files which have not already been removed from the working directory are not processed). This command does not actually remove the file from the repository until you commit the removal. For a full list of options, see section Quick reference to CVS commands.

Here is an example of removing several files:

    $ cd test
    $ rm *.c
    $ cvs remove
    cvs remove: Removing .
    cvs remove: scheduling a.c for removal
    cvs remove: scheduling b.c for removal
    cvs remove: use 'cvs commit' to remove these files permanently
    $ cvs ci -m "Removed unneeded files"
    cvs commit: Examining .
    cvs commit: Committing .

As a convenience you can remove the file and `cvs remove` it in one step, by specifying the ``-f'` option. For example, the above example could also be done like this:

    $ cd test
    $ cvs remove -f *.c
    cvs remove: scheduling a.c for removal
    cvs remove: scheduling b.c for removal
    cvs remove: use 'cvs commit' to remove these files permanently
    $ cvs ci -m "Removed unneeded files"
    cvs commit: Examining .
    cvs commit: Committing .

If you execute `remove` for a file, and then change your mind before you commit, you can undo the `remove` with an `add` command.

    $ ls
    CVS   ja.h  oj.c
    $ rm oj.c
    $ cvs remove oj.c
    cvs remove: scheduling oj.c for removal
    cvs remove: use 'cvs commit' to remove this file permanently
    $ cvs add oj.c
    U oj.c
    cvs add: oj.c, version 1.1.1.1, resurrected

If you realize your mistake before you run the `remove` command you can use `update` to resurrect the file:

    $ rm oj.c
    $ cvs update oj.c
    cvs update: warning: oj.c was lost
    U oj.c

When you remove a file it is removed only on the branch which you are working on (see section Branching and merging). You can later merge the removals to another branch if you want (see section Merging can add or remove files).

## Removing directories

In concept removing directories is somewhat similar to removing files--you want the directory to not exist in your current working directories, but you also want to be able to retrieve old releases in which the directory existed.

The way that you remove a directory is to remove all the files in it. You don't remove the directory itself; there is no way to do that. Instead you specify the ``-P'` option to `cvs update`, `cvs checkout`, or `cvs export`, which will cause CVS to remove empty directories from working directories. Probably the best way to do this is to always specify ``-P'`; if you want an empty directory then put a dummy file (for example ``.keepme'`) in it to prevent ``-P'` from removing it.

Note that ``-P'` is implied by the ``-r'` or ``-D'` options of `checkout` and `export`. This way CVS will be able to correctly create the directory or not depending on whether the particular version you are checking out contains any files in that directory.

## Moving and renaming files

Moving files to a different directory or renaming them is not difficult, but some of the ways in which this works may be non-obvious. (Moving or renaming a directory is even harder. See section Moving and renaming directories.).

The examples below assume that the file old is renamed to new.

- Outside: The normal way to Rename
- Inside: A tricky, alternative way
- Rename by copying: Another tricky, alternative way

### The Normal way to Rename

The normal way to move a file is to copy old to new, and then issue the normal CVS commands to remove old from the repository, and add new to it.

    $ mv old new
    $ cvs remove old
    $ cvs add new
    $ cvs commit -m "Renamed old to new" old new

This is the simplest way to move a file, it is not error-prone, and it preserves the history of what was done. Note that to access the history of the file you must specify the old or the new name, depending on what portion of the history you are accessing. For example, `cvs log old` will give the log up until the time of the rename.

When new is committed its revision numbers will start again, usually at 1.1, so if that bothers you, use the ``-r rev'` option to commit. For more information see section Assigning revisions.

### Moving the history file

This method is more dangerous, since it involves moving files inside the repository. Read this entire section before trying it out!

    $ cd $CVSROOT/module
    $ mv old,v new,v

Advantages:

- The log of changes is maintained intact.
- The revision numbers are not affected.

Disadvantages:

- Old releases of the module cannot easily be fetched from the repository. (The file will show up as new even in revisions from the time before it was renamed).
- There is no log information of when the file was renamed.
- Nasty things might happen if someone accesses the history file while you are moving it. Make sure no one else runs any of the CVS commands while you move it.

### Copying the history file

This way also involves direct modifications to the repository. It is safe, but not without drawbacks.

    # Copy the RCS file inside the repository
    $ cd $CVSROOT/module
    $ cp old,v new,v
    # Remove the old file
    $ cd ~/module
    $ rm old
    $ cvs remove old
    $ cvs commit old
    # Remove all tags from new
    $ cvs update new
    $ cvs log new             # Remember the non-branch tag names
    $ cvs tag -d tag1 new
    $ cvs tag -d tag2 new
    ...

By removing the tags you will be able to check out old revisions of the module.

Advantages:

- Checking out old revisions works correctly, as long as you use ``-rtag'` and not ``-Ddate'` to retrieve the revisions.
- The log of changes is maintained intact.
- The revision numbers are not affected.

Disadvantages:

- You cannot easily see the history of the file across the rename.

## Moving and renaming directories

The normal way to rename or move a directory is to rename or move each file within it as described in section The Normal way to Rename. Then check out with the ``-P'` option, as described in section Removing directories.

If you really want to hack the repository to rename or delete a directory in the repository, you can do it like this:

1. Inform everyone who has a copy of the module that the directory will be renamed. They should commit all their changes, and remove their working copies of the module, before you take the steps below.
2. Rename the directory inside the repository.

       $ cd $CVSROOT/module
       $ mv old-dir new-dir

3. Fix the CVS administrative files, if necessary (for instance if you renamed an entire module).
4. Tell everyone that they can check out the module and continue working.

If someone had a working copy of the module the CVS commands will cease to work for him, until he removes the directory that disappeared inside the repository.

It is almost always better to move the files in the directory instead of moving the directory. If you move the directory you are unlikely to be able to retrieve old releases correctly, since they probably depend on the name of the directories.

Go to the first, previous, next, last section, table of contents.