Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Perforce cheatsheet

📅 2018-Feb-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cheatsheet, p4 ⬩ 📚 Archive

Perforce is an old version control system popular in big companies. p4 is its primary commandline tool. p4g is its GUI tool. The reference for all the commands in my cheatsheet is the p4 command reference.

$ p4 help foobar
$ p4 depots
$ p4 info
$ p4 set
$ p4 clients
$ p4 clients -u joe
$ p4 client

You typically use this command to add new branches or paths from the depot to your directory view.

$ p4 client my_client_2
$ p4 client -d my_client_2
$ p4 sync
$ p4 sync -n
$ p4 sync //dev/foo/some.cpp
$ p4 sync ...@123456

Copy, move and delete

$ p4 copy old_file new_file
$ p4 copy old_dir/... new_dir/...
$ p4 move old_file new_file

If the move operation is disabled for your Perforce server, then check out these alternate solutions.

$ p4 delete foobar_file
$ p4 delete foobar_dir/...


p4 commands related to changelist are collected here.


$ p4 changes

This will list all changelists of everybody, so it is pretty much useless. You want to filter it down using other criteria, like shown below.

$ p4 changes //somedepot/
$ p4 changes //somedepot/somebranch/
$ p4 changes -m5
$ p4 changes -u joe
$ p4 changes -l
$ p4 changes -L
$ p4 changes -s pending
$ p4 changes -m1 ...#have
$ p4 add foobar.txt

If it is a new file, then you will need to create the file yourself after this command. This command just indicates to p4 to start tracking such a file.

$ p4 delete foobar.txt
$ p4 delete -c 123456 foobar.txt
$ p4 edit foobar.txt
$ p4 revert foobar.txt
$ p4 revert ...
$ p4 revert src/...

Note that you will lose changed made your files on disk with this operation. This operation can be used after shelving a changelist to revert the files back to their original state.

$ p4 revert -a
$ p4 opened
$ p4 opened ...
$ p4 opened src/...

File information

$ p4 changes //somedepot/somepath/foobar.txt
$ p4 filelog //somedepot/somepath/foobar.txt
$ p4 print //somedepot/somepath/foobar.txt
$ p4 annotate //somedepot/somepath/foobar.txt
$ p4 annotate -c //somedepot/somepath/foobar.txt
$ p4 annotate -u //somedepot/somepath/foobar.txt


$ p4 diff

This prints a text diff at the shell by default. If you set the P4DIFF environment variable to a GUI program, like Meld, it will use that to show the diffs.

$ p4 diff -du
$ p4 diff -sa
$ p4 diff2 //branch1/some.cpp //branch2/some.cpp

This prints a diff at the shell. Set the P4DIFF2 environment variable to a GUI diff program to view the diff using that.


To create a new branch from an old branch, there are typically 3 steps:

First, create the branch:

$ p4 integrate //foo/old_branch/... //foo/new_branch/...

Now the branch is created, but you cannot yet view the files in the new branch in your filesystem.

Second, change the client specification add the new branch files to your view and sync:

$ p4 client
$ p4 sync

Finally, submit the new branch to depot:

$ p4 submit //foo/new_branch/...


$ p4 integrate //foo/from_branch/... //foo/my_branch/...
$ p4 resolve -a
$ p4 resolve
$ p4 resolve -n
$ p4 integrate from_branch/...@=123456 to_branch/...
$ p4 resolve to_branch/...
$ p4 change