Clone
Generally, git p4 clone is used to create a new Git directory
from an existing p4 repository:
$ git p4 clone //depot/path/project
This:
1. Creates an empty Git repository in a subdirectory called
project.
2. Imports the full contents of the head revision from the given
p4 depot path into a single commit in the Git branch
refs/remotes/p4/master.
3. Creates a local branch, master from this remote and checks it
out.
To reproduce the entire p4 history in Git, use the @all modifier
on the depot path:
$ git p4 clone //depot/path/project@all
Sync
As development continues in the p4 repository, those changes can
be included in the Git repository using:
$ git p4 sync
This command finds new changes in p4 and imports them as Git
commits.
P4 repositories can be added to an existing Git repository using
git p4 sync too:
$ mkdir repo-git
$ cd repo-git
$ git init
$ git p4 sync //path/in/your/perforce/depot
This imports the specified depot into refs/remotes/p4/master in
an existing Git repository. The --branch
option can be used to
specify a different branch to be used for the p4 content.
If a Git repository includes branches refs/remotes/origin/p4,
these will be fetched and consulted first during a git p4 sync.
Since importing directly from p4 is considerably slower than
pulling changes from a Git remote, this can be useful in a
multi-developer environment.
If there are multiple branches, doing git p4 sync will
automatically use the "BRANCH DETECTION" algorithm to try to
partition new changes into the right branch. This can be
overridden with the --branch
option to specify just a single
branch to update.
Rebase
A common working pattern is to fetch the latest changes from the
p4 depot and merge them with local uncommitted changes. Often,
the p4 repository is the ultimate location for all code, thus a
rebase workflow makes sense. This command does git p4 sync
followed by git rebase to move local commits on top of updated p4
changes.
$ git p4 rebase
Submit
Submitting changes from a Git repository back to the p4
repository requires a separate p4 client workspace. This should
be specified using the P4CLIENT
environment variable or the Git
configuration variable git-p4.client. The p4 client must exist,
but the client root will be created and populated if it does not
already exist.
To submit all changes that are in the current Git branch but not
in the p4/master branch, use:
$ git p4 submit
To specify a branch other than the current one, use:
$ git p4 submit topicbranch
To specify a single commit or a range of commits, use:
$ git p4 submit --commit <sha1>
$ git p4 submit --commit <sha1..sha1>
The upstream reference is generally refs/remotes/p4/master, but
can be overridden using the --origin=
command-line option.
The p4 changes will be created as the user invoking git p4
submit. The --preserve-user
option will cause ownership to be
modified according to the author of the Git commit. This option
requires admin privileges in p4, which can be granted using p4
protect.
To shelve changes instead of submitting, use --shelve
and
--update-shelve
:
$ git p4 submit --shelve
$ git p4 submit --update-shelve 1234 --update-shelve 2345
Unshelve
Unshelving will take a shelved P4 changelist, and produce the
equivalent git commit in the branch
refs/remotes/p4-unshelved/<changelist>.
The git commit is created relative to the current origin revision
(HEAD by default). A parent commit is created based on the
origin, and then the unshelve commit is created based on that.
The origin revision can be changed with the "--origin" option.
If the target branch in refs/remotes/p4-unshelved already exists,
the old one will be renamed.
$ git p4 sync
$ git p4 unshelve 12345
$ git show p4-unshelved/12345
<submit more changes via p4 to the same files>
$ git p4 unshelve 12345
<refuses to unshelve until git is in sync with p4 again>