Previous Table of Contents Index Next
Perforce 2009.2: P4 User's Guide

Chapter 4
This chapter tells you how to manage files and work in a team development environment, where multiple users who are working on the same files might need to reconcile their changes.
Managing files
To change files in the depot (file repository), you open the files in changelists and submit the changelists with a description of your changes. Perforce assigns numbers to changelists and maintains the revision history of your files. This approach enables you to group related changes and find out who changed a file and why and when it was changed. Here are the basic steps for working with files.
Syncing (retrieving files from the depot)
Issue the p4 sync command, specifying the files and directories you want to retrieve from the depot. You can only sync files that are mapped in your client view.
Adding files to the depot
Editing files and checking in changes
Submit the changelist (p4 submit). To discard changes, issue the p4 revert command.
Deleting files from the depot
Open the file for delete in a changelist (p4 delete). The file is deleted from your workspace.
Submit the changelist (p4 submit). The file is deleted from the depot.
Add: no effect—the file remains in your workspace.
Edit: the revision you opened is resynced from the depot, overwriting any changes you made to the file in your workspace.
Files are added to, deleted from, or updated in the depot only when you successfully submit the pending changelist in which the files are open. A changelist can contain a mixture of files open for add, edit and delete.
For details about the syntax that you use to specify files on the command line, refer to Specifying filenames on the command line. The following sections provide more details about working with files.
Syncing (retrieving) files
To retrieve files from the depot into your client workspace, issue the p4 sync command. You cannot sync files that are not in your client view. For details about specifying client views, see Refining client views.
Example: Copying files from the depot to a client workspace
The following command retrieves the most recent revisions of all files in the client view from the depot into the workspace. As files are synced, they are listed in the command output.
//depot/dev/main/bin/bin.linux24x86/readme.txt#1 - added as c:\bruno_ws\dev\main\bin\bin.linux24x86\readme.txt
//depot/dev/main/bin/bin.ntx86/glut32.dll#1 - added as c:\bruno_ws\dev\main\bin\bin.ntx86\glut32.dll
//depot/dev/main/bin/bin.ntx86/jamgraph.exe#2 - added as c:\bruno_ws\dev\main\bin\bin.ntx86\jamgraph.exe
The p4 sync command adds, updates, or deletes files in the client workspace to bring the workspace contents into agreement with the depot. If a file exists within a particular subdirectory in the depot, but that directory does not exist in the client workspace, the directory is created in the client workspace when you sync the file. If a file has been deleted from the depot, p4 sync deletes it from the client workspace.
To sync revisions of files prior to the latest revision in the depot, use revision specifiers. For example, to sync the first revision of Jamfile, which has multiple revisions, issue the following command:
p4 sync//depot/dev/main/jam/Jamfile#1
For more details, refer to Specifying file revisions.
To sync groups of files or entire directories, use wildcards. For example, to sync everything in and below the "jam" folder, issue the following command:
p4 sync //depot/dev/main/jam/...
For more details, see Perforce wildcards.
The Perforce server tracks the revisions that you sync (in a database located on the server machine). For maximum efficiency, Perforce does not resync an already-synced file revision. To resync files you (perhaps inadvertently) deleted manually, specify the -f flag when you issue the p4 sync command.
Adding files
To add files to the depot, create the files in your workspace, then issue the p4 add command. The p4 add command opens the files for add in the default pending changelist. The files are added when you successfully submit the default pending changelist. You can open multiple files for add using a single p4 add command by using wildcards. You cannot use the Perforce ...wildcard to add files recursively.
For platform-specific details about adding files recursively (meaning files in subdirectories), see Tech Note 12 on the Perforce web site:
Example: Adding files to a changelist
Bruno has created a couple of text files that he needs to add to the depot. To add all the text files at once, he uses the "*" wildcard when he issues the p4 add command.
C:\bruno_ws\dev\main\docs\manuals>p4 add *.txt
//depot/dev/main/docs/manuals/installnotes.txt#1 - opened for add
//depot/dev/main/docs/manuals/requirements.txt#1 - opened for add
Now the files he wants to add to the depot are open in his default changelist. The files are stored in the depot when the changelist is submitted.
Example: Submitting a changelist to the depot
Bruno is ready to add his files to the depot. He types p4 submit and sees the following form in a standard text editor:
Change: new
Client: bruno_ws
User:   bruno
Status: new
        <enter description here>
    //depot/dev/main/docs/manuals/installnotes.txt   # add
    //depot/dev/main/docs/manuals/requirements.txt   # add
Bruno changes the contents of the Description: field to describe his file updates. When he's done, he saves the form and exits the editor, and the new files are added to the depot.
You must enter a description in the Description: field. You can delete lines from the Files: field. Any files deleted from this list are moved to the next default changelist, and are listed the next time you submit the default changelist.
If you are adding a file to a directory that does not exist in the depot, the depot directory is created when you successfully submit the changelist.
Changing files
To open a file for edit, issue the p4 edit command. When you open a file for edit, Perforce enables write permission for the file in your workspace and adds the file to a changelist. If the file is in the depot but not in your workspace, you must sync it before you open it for edit. You must open a file for edit before you attempt to edit the file.
Example: Opening a file for edit
Bruno wants to make changes to command.c, so he syncs it and opens the file for edit.
p4 sync //depot/dev/command.c
//depot/dev/command.c#8 - added as c:\bruno_ws\dev\command.c
p4 edit //depot/dev/command.c
//depot/dev/command.c#8 - opened for edit
He then edits the file with any text editor. When he's finished, he submits the file to the depot with p4 submit, as described above.
Discarding changes (reverting)
To remove an open file from a changelist and discard any changes you made, issue the p4 revert command. When you revert a file, the Perforce server restores the last version you synced to your workspace. If you revert a file that is open for add, the file is removed from the changelist but is not deleted from your workspace.
Example: Reverting a file
Bruno decides not to add his text files after all.
//depot/dev/main/docs/manuals/installnotes.txt#none - was add, abandoned
//depot/dev/main/docs/manuals/requirements.txt#none - was add, abandoned
To preview the results of a revert operation without actually reverting files, specify the -n flag when you issue the p4 revert command.
Deleting files
To delete files from the depot, you open them for delete by issuing the p4 delete command, then submit the changelist in which they are open. When you delete a file from the depot, previous revisions remain, and a new head revision is added, marked as "deleted." You can still sync previous revisions of the file.
When you issue the p4 delete command, the files are deleted from your workspace but not from the depot. If you revert files that are open for delete, they are restored to your workspace. When you successfully submit the changelist in which they are open, the files are deleted from the depot.
Example: Deleting a file from the depot
Bruno deletes vendor.doc from the depot as follows:
p4 delete //depot/dev/main/docs/manuals/vendor.doc
//depot/dev/main/docs/manuals/vendor.doc#1 - opened for delete
The file is deleted from the client workspace immediately, but it is not deleted from the depot until he issues the p4 submit command.
Managing changelists
To change files in the depot, you open them in a changelist, make any changes to the files, and then submit the changelist. A changelist contains a list of files, their revision numbers, and the operations to be performed on the files. Unsubmitted changelists are referred to as pending changelists.
Submission of changelists is an all-or-nothing operation; that is, either all of the files in the changelist are updated in the depot, or, if an error occurs, none of them are. This approach guarantees that code alterations that affect multiple files occur simultaneously.
Perforce assigns numbers to changelists and also maintains a default changelist, which is numbered when you submit it. You can create multiple changelists to organize your work. For example, one changelist might contain files that are changed to implement a new feature, and another changelist might contain a bug fix. When you open a file, it is placed in the default changelist unless you specify an existing changelist number on the command line using the -c flag. For example, to edit a file and submit it in changelist number 4, use p4 edit -c 4 filename. To open a file in the default changelist, omit the
-c flag
You can also shelve changelists in order to temporarily preserve work in progress for your own use, or for review by others. Shelving enables you to temporarily cache files on the central server without submitting it to the depot.
The Perforce server might renumber a changelist when you submit it, depending on other users' activities; if your changelist is renumbered, its original number is never reassigned to another changelist.
The commands that add or remove files from changelists are:
To submit a numbered changelist, specify the -c flag when you issue the p4 submit command. To submit the default changelist, omit the -c flag. For details, refer to the p4 submit command description in the Perforce Command Reference.
To move files from one changelist to another, issue the p4 reopen -c changenum filenames command, where changenum specifies the number of the target changelist. If you are moving files to the default changelist, use p4 reopen -c default filenames.
Creating numbered changelists
To create a numbered changelist, issue the p4 change command. This command displays the changelist form. Enter a description and make any desired changes; then save the form and exit the editor.
All files open in the default changelist are moved to the new changelist. When you exit the text editor, the changelist is assigned a number. If you delete files from this changelist, the files are moved back to the default changelist.
Example: Working with multiple changelists
Bruno is fixing two different bugs, and needs to submit each fix in a separate changelist. He syncs the head revisions of the files for the first fix and opens the for edit in the default changelist
C:\bruno_ws>p4 edit //depot/dev/main/jam/*.c
[list of files opened for edit...]
Now he issues the p4 change command and enters a description in the changelist form. After he saves the file and exits the editor, Perforce creates a numbered changelist containing the files.
    [Enter description and save form]
For the second bug fix, he performs the same steps, p4 sync, p4 edit, and p4 change. Now he has two numbered changelists, one for each fix.
The numbers assigned to submitted changelists reflect the order in which the changelists were submitted. When a changelist is submitted, the Perforce server might renumber it, as shown in the following example.
Example: Automatic renumbering of changelists
Bruno has finished fixing the bug that he's been using changelist 777 for. After he created that changelist, he submitted another changelist, and two other users also submitted changelists. Bruno submits changelist 777 with p4 submit -c 777, and sees the following message:
Change 777 renamed change 783 and submitted.
Submitting changelists
To submit a pending changelist, issue the p4 submit command. When you issue the p4 submit command, a form is displayed, listing the files in the changelist. You can remove files from this list. The files you remove remain open in the default pending changelist until you submit them or revert them.
To submit specific files that are open in the default changelist, issue the p4 submit filename command. To specify groups of files, use wildcards. For example, to submit all text files open in the default changelist, type p4 submit "*".txt. (Use quotation marks as an escape code around the * wildcard to prevent it from being interpreted by the local command shell).
After you save the changelist form and exit the text editor, the changelist is submitted to the Perforce server, and the server updates the files in the depot. After a changelist has been successfully submitted, only a Perforce administrator can change it, and the only fields that can be changed are the description and user name.
If an error occurs when you submit the default changelist, Perforce creates a numbered changelist containing the files you attempted to submit. You must then fix the problems and submit the numbered changelist using the -c flag.
Perforce enables write permission for files that you open for edit and disables write permission when you successfully submit the changelist containing the files. To prevent conflicts with the Perforce server's management of your workspace, do not change file write permissions manually.
Deleting changelists
To delete a pending changelist, you must first remove all files and jobs associated with it and then issue the p4 change -d changenum command. Related operations include the following:
To move files to another changelist, issue the p4 reopen -c changenum command.
Changelists that have already been submitted can be deleted only by a Perforce administrator. See the Perforce System Administrator's Guide for more information.
Renaming and moving files
To rename or move files, you must first open them for add or edit, and then use the p4 move command:
p4 move source_file target_file
To move groups of files, use matching wildcards in the source_file and target_file specifiers. To move files, you must have Perforce write permission for the specified files. (For details about Perforce permissions, see the Perforce System Administrator's Guide.)
When you rename or move a file using p4 move, the Perforce server creates an integration record that links it to its deleted predecessor, preserving the file's history. (Integration is also used to create branches and to propagate changes. For details, see Integrating changes.
Shelving work in progress
The Perforce shelving feature enables you to temporarily store copies of your files on the Perforce server without checking the changelist into the depot.
Shelving is useful for individual developers who are switching between tasks or performing cross-platform testing before checking in their changes. Shelving also enables teams to easily hand off changes and to perform code reviews.
Example: Shelving a changelist
Earl has made changes to command.c on a UNIX platform, and now wants others to be able to view and test his changes.
$ p4 edit //depot/dev/command.c
//depot/dev/command.c#9 - opened for edit
Change 123 created with 1 open file(s).
Shelving files for change 123.
edit //depot/dev/command.c#9
Change 123 files shelved.
A pending changelist is created, and the shelved version of command.c is stored on the server. The file command.c remains editable in Earl's workspace, and Earl can continue to work on the file, or can revert his changes and work on something else.
Shelved files remain open in the changelist from which they were shelved. You can continue to work on the files in your workspace without affecting the shelved files. Shelved files can be synced to other workspaces, including workspaces owned by other users. For example:
Example: Unshelving a changelist for code review
Earl has asked for code review and a cross-platform compatibility check on the version of command.c that he shelved in changelist 123. Bruno, who is using a Windows machine, types:
C:\bruno_ws\dev> p4 unshelve -s 123 //depot/dev/command.c
//depot/dev/command.c#9 - unshelved, opened for edit
and conducts the test in the Windows environment while Earl continues on with other work.
When you shelve a file, the version on the shelf is unaffected by commands that you perform in your own workspace, even if you revert the file to work on something else.
Example: Handing off files to other users
Earl's version of command.c works on UNIX, but Bruno's cross-platform check of command.c has revealed a bug. Bruno can take over the work from here, so Earl reverts his workspace and works on something else:
$ p4 revert //depot/dev/command.c
//depot/dev/command.c#9 - was edit, reverted
The shelved version of command.c is still available from Earl's pending changelist 123, and Bruno opens it in a new changelist, changelist 124.
$ p4 edit -s 123 -c 124 //depot/dev/command.c
//depot/dev/command.c#9 - unshelved, opened for edit
When Bruno is finished with the work, he can either re-shelve the file (in his own changelist 124, not Earl's changelist 123) for further review, or discard the shelved file and submit the version in his workspace by using p4 submit.
Because shelved files are still open, you cannot submit a changelist that refers to shelved files. Before you submit a changelist, you must either discard the shelved files, or move the shelved files into a new pending changelist.
Example: Discarding shelved files before submitting a change.
The Windows cross-platform changes are complete, and changelist 124 is ready to be submitted. Bruno uses p4 shelve -d to discard the shelved files.
All files in the shelved changelist are deleted. Bruno can now submit the changelist.
Bruno could have shelved the file in changelist 124, and let Earl unshelve it back into his original changelist 123 to complete the check-in.
Displaying information about changelists
To display brief information about changelists, use the p4 changes command. To display full information, use the p4 describe command. The following table describes some useful reporting commands and options.
Displays a list of all pending and submitted changelists, one line per changelist, and an abbreviated description.
Limits the list to those changelists with a particular status; for example, p4 changes -s submitted lists only already submitted changelists.
p4 describe changenum
Displays full information about a single changelist. If the changelist has already been submitted, the report includes a list of affected files and the diffs of these files. (You can use the -s flag to exclude the file diffs.)
For more information, see Changelist reporting.
Diffing files
Perforce provides a program that enables you to diff (compare) revisions of text files. By diffing files, you can display:
To diff a file that is synced to your workspace with a depot revision, issue the p4 diff filename#rev command. If you omit the revision specifier, the file in your workspace is compared with the revision you last synced, to display changes you made after syncing it.
To diff two revisions that reside in the depot but not in your workspace, use the p4 diff2 command. To diff a set of files, specify wildcards in the filename argument when you issue the p4 diff2 command.
The p4 diff command performs the diff on your client machine, but the p4 diff2 command performs the diff on the server and sends the results to your client machine.
The following table lists some common uses for diff commands.
The head revision
p4 diff file
p4 diff file#head
p4 diff file#3
The head revision
p4 diff2 file file#134
File revision at changelist 32
File revision at changelist 177
p4 diff2 file@32 file@177
A file shelved in pending changelist 123
All files in release 1
All files in release 2
By default, the p4 diff command launches the Perforce client's internal diff program. To use a different diff program, set the P4DIFF environment variable to specify the path and executable of the desired program. To specify arguments for the external diff program, use the -d flag. For details, refer to the Perforce Command Reference.
Working offline
If you need to work offline (without access to your Perforce server) on files under Perforce control, you must reconcile your work with the Perforce server when you regain access to the server. The following method for working detached assumes that you work on files in your client workspace or update the workspace with your additions, changes, and deletions before you update the depot.
For platform-specific details about working detached, see Tech Note 2 on the Perforce web site:
To work offline:
Work on files without issuing p4 commands. Instead, use operating system commands to change the permissions on files.
After the network connection is reestablished, use p4 diff to find all files in your workspace that have changed. (You need to track new files manually.)
The following sections provide more details.
Finding changed files
To detect changed files, issue the p4 diff command. The following flags enable you to locate files that you changed or deleted manually, without opening them for edit or delete in Perforce.
Lists workspace files that are not open for edit but have been changed since being synced. To update the depot with these files, open them for edit and submit them.
Lists files that have been manually deleted from the workspace. To update the depot with these file deletions, open them for delete and submit them.
Submitting your changes
To update the depot with the changes that you made to the client workspace while working detached, use the p4 diff flags described above with the -x flag, as shown in the following examples. The -x flag directs the p4 edit command to accept arguments from the pipe (or a file).
To open changed files for edit after working detached, issue the following command:
p4 diff -se | p4 -x - edit
To delete files from the depot that were removed from the client workspace, issue the following command:
p4 diff -sd | p4 -x - delete
Open any new files for add; then submit the changelist containing your additions, changes, and deletions.

Previous Table of Contents Index Next

Perforce 2009.2: P4 User's Guide
Copyright 2005-2009 Perforce Software.