How to sync locally modified file with server revision in perforce - perforce

I'm updating a locally modified file with the server revision so that I have all the latest changes (that other developers made while I was working on the file). I've already tried p4 sync. Does anyone know the correct way to do deal with this?
Thanks

If the file is opened for edit, and you have already run 'p4 sync', then you should have seen a message like:
$ p4 sync
//depot/main/b#2 - is opened and not being changed
... //depot/main/b - must resolve #2 before submitting
What this means is that Perforce is ready for you to merge your changes together with the changes from the new revision.
Perforce calls this process "resolving" the changes, and has told you that you must resolve them before submitting the file.
When you are ready to merge your changes with the new changes from the new revision, run:
$ p4 resolve
Many people find this process of merging the changes a bit complicated, and prefer to use a GUI tool. Try downloading the P4V tool from the Perforce website and it will help you merge the changes using a visual merge tool.
If you instead decide that you do not want to keep your local changes, and would prefer to discard them, and use the latest version of the file instead, you can discard your changes by running:
$ p4 revert
But be careful! This will lose all the unsubmitted changes that you have made to your file! The same is true of the 'sync -f' command and the 'p4 clean' command; these commands tell Perforce that you don't want your locally-made unsubmitted changes, and Perforce should replace the file with a clean copy from the server.

I will add more detail on Bryan's answer especially about all sequence of syncing, and fixing merge conflicts; assume that this is based on CLI p4.
Let's say you have locally modified files that upstream also has some updates for the same files since you've modified it locally.
Sanity steps I would do is the following
p4 sync -n :: with -n this is dry-run which it won't actually have any effect or perform anything yet, but will return output if it really performs. For the specific situation we're in right now, you probably want to look for the line that has ... in front + the line above it which says is opened and not being changed. With our situation, this means upstream files has updates for the file you've opened and probably made some changes to it. It needs to be resolved.
At this point, you can execute p4 sync to actually perform it.
p4 resolve -n :: again with -n which means dry-run. This is to check whether there's any outstanding conflicts you need to resolve as the result of your sync.
(if the output from 3. is not No file(s) to resolve.) p4 resolve -am :: this will perform conflict resolution automatically. It will try merging but will not do anything if there's any merge conflicts for target file. Its output will list out the result of each file. For files that it leaves out, there will be non-zero conflicts in the output.
p4 resolve -af :: perform merging manually. Its output will list out files (of course with their path).
From 5, edit each files as seen in the output. Search for ORIGINAL or THEIRS or YOURS then delete unwanted section, or merge things together as needed. When finish for each file, just save and quit. Do this for all files.
PS. More info for 6. Actually you can specify which merging resolution policy you want it to happen in which it can be
p4 resolve -at :: accept changes from upstream (accept THEIRS)
p4 resolve -ay :: ignore changes from upstream, only accept what you have locally (accept YOURS)
Also keep in mind, THEIRS doesn't need to always be upstream changes from depot, but if can mean a changelist that you just unshelved into your workspace locally.

Related

Make the contents of a perforce directory exactly match the latest depot version

I would like to make the contents of a directory in a Perforce workspace be exactly the same as the latest depot contents. I want this to work no matter what has gone on in the workspace. (For example, editing files without opening them in perforce, creating unsubmitted changesets, etc.)
So far I have come up with running this series of commands, in order:
p4 clean ...
p4 revert ...
p4 sync ...
Will this do what I want? Is this the correct order? Is there a simpler way?
Those are the correct commands, and you do need all three:
revert is needed to discard changes to files you've opened
clean is needed to discard changes to files you haven't opened (including added files)
sync is needed to make sure you're at the very latest depot revision
You should either put clean after the revert or add -w to revert; otherwise files that were opened for add will be skipped by the clean (because they're open) and then revert will "abandon" them in the workspace (the -w option causes revert to delete/"wipe" added files instead of abandoning them). Other than that, I think the order is unimportant.

In perforce how can I undo a resolve -am?

P4D/LINUX26X86_64/2013.2/938876 (2014/09/23)
I had some changes in the default changelist that I tried to shelve. It said I couldn't because I needed to resolve with some other changelist. It stuck my files in some other changelist. After looking at the differences, I saw that I wanted to keep all my changes and accept nothing from the other changelist. Thinking that "-am" meant "accept mine", I ran "p4 resolve -am". Then there was a "Duuuuhhhhh" momemt when I remembered that "-am" means "auto-merge".
Q: How can I "undo" the automerge that just happened so as to restore what I had ?
If you were able to shelve successfully at some point you can get the pre-resolve files back from the shelf via "p4 unshelve".
As a general rule you can't undo the results of a resolve operation to get the "yours" file back, since the workspace file isn't backed up anywhere.
Since this was an automerge, one option would be to try to run the merge in reverse. You started with A(base)+AB(theirs)+AC(yours), and now you have ABC(merged) -- so you want to set up a merge that'll produce AC. That'd be AB(base)+A(theirs)+ABC(yours)=AC(merged) -- basically you want to find the "base" and "theirs" from the automerge and reverse them, using the merged result as "yours". Run "p4 resolved -o" to find the "base" and "theirs" file from the previous merge, then run "p4 print" to get those file contents and use "p4 merge3" to run the 3-way merge algorithm with the three appropriate inputs and the output should be your original file (in theory).

How to only receive get file changed by revision In perforce P4

In P4, I only want to pull file changed my client workspace from server. But When I use "p4 sync", this command get all files from server. So, How to get files changed from list revision ? with the files were existed on local and not changed, the command P4 sync do not need sync. My command is here:
p4 sync -f //depot/...#Revision
If you want to restore a file you deleted with p4 delete file, use p4 revert file, not p4 sync. p4 sync is for getting the changes that other people made to the files, not for altering the changes that you made.
There are three different reasons that the files in your workspace might differ from the current head revision in the depot:
A new revision has been submitted to the depot since the last time you synced.
You have opened the files (e.g. with p4 edit) and made changes to them that are not yet submitted.
You have modified the read-only synced files in your workspace without opening them.
(You don't say in your question which of these is the case, which is why you've gotten a couple of different answers that are based on different assumptions about your workspace state -- I'm guessing it's #3, personally, but I'll give the answer for each one so you can understand how it's "supposed" to work vs what's actually going on here.)
In case 1, a normal p4 sync command will update only the files with new revisions. If the revision you previously synced is the same as the head revision, p4 sync won't update it. This makes p4 sync very fast -- you can have a million files in your workspace, and if only one of them has changed since you last sync, only that one file is affected.
In case 2, no p4 sync command will update the files, in the interest of preserving your open changes. To submit your changes to the depot, use p4 submit; to discard them, use p4 revert. Again, only the open files are affected by these commands, so a revert operation affecting only a few files isn't slowed down by however many other files are in your workspace.
In case 3, your workspace is in an inconsistent state -- Perforce's standard workflow is that any time you modify a local file you should "open" it so that the server can track your local work (and optimize operations like sync, submit, etc, as well as alert other users who open those files simultaneously to potential conflicts). If you modify a file locally without modifying it, commands like sync will no longer work as well because the state of your client is not known to the server.
You can recover from an inconsistent state by using the p4 reconcile command (which will open all the inconsistent files so that you can either submit or revert them), or the p4 clean command (which irrevocably discards the inconsistent local changes, as if you'd done p4 reconcile immediately followed by p4 revert). These commands are significantly slower than a normal sync operation since they need to scan the entire workspace rather than only the changed files, but they are still significantly faster than a sync -f since only the changed files are actually re-transferred.

Perforce p4v Merge/Integrate freeze after file rename

I'm having trouble performing a merge/integrate from branch1/sql/ to b2/sql
I performed a rename operation in p4v from _1.sql to _2.sql
Made a small change to _2 file
Submitted changes
Went to submitted changes, and tried to perform a merge/integrate on _2 to the other branch (b2/sql).
The problem is that p4v freezes at that point:
What am I doing wrong ?
BTW, I have the latest version: Version: Helix P4V/NTX64/2018.2/1666551
Based on the generated changelist description, P4V appears to be hopelessly confused and trying to integrate the file into itself rather than between the two different branches you specified.
Easiest fix is to run it from the command line:
p4 merge //depot/Engineering/INT-DEV/...#=CHANGE //depot/Engineering/projects/...
where CHANGE is the small change you're trying to merge (this is easier/safer than specifying the full file path, especially if you're dealing with a file that got renamed in one branch but not the other since it's otherwise easy to mess up entering one of the paths).
If the small change is the only change you've made since the last merge, you can just trust p4 to figure that out automatically and do this very simple command instead:
p4 merge //depot/Engineering/INT-DEV/... //depot/Engineering/projects/...

Reverting multiple checkins in perforce

I have made several checkins using perforce. I have no realized that all of them are unnecessary. I would like to revert all the changes for the last x revisions in the working directory, update the version number, and check in.
I am familiar with Mercurial. The way that I would it for that would be:
$ hg revert -r last_good_changeset .
$ edit version-number.txt
$ hg ci
Is there a way to do something similar in perforce?
In Perforce, a revert refers to restoring a file to the state it was in before it was checked out. What you're looking to do is back out a submitted changelist. This Perforce KB article has a few methods to do what you're trying to do, depending on your particular circumstance.
For example, if you have revisions #1 - #6 of a particular file, and you want to roll back to revision #3, you'd do this:
p4 sync myfilename#3
p4 edit myfilename
p4 sync myfilename
You're telling Perforce to get revision #3 from the depot, check it out for edit, then try to sync it back up to #head (the latest version in the depot). Since the file is checked out from an earlier revision, Perforce schedules a resolve so you need to tell it what you want to do with the file: accept the version in the depot, accept your local changes, or try to merge the two. You'll want to tell Perforce to accept the local version (or in Perforce parlance "yours"):
p4 resolve -ay myfilename
Now that it's resolved, you can submit it with:
p4 submit
If you have a series of files you want to do this with (for example, you've edited a bunch of files in a given directory and have checked them all in together several times, and you want to back out all of those), you can use changelist syntax as well. For example, if you want to roll everything back in a given directory to changelist 123, you can do this:
p4 sync //depot/some/path/*#123
p4 edit //depot/some/path/*
p4 sync //depot/some/path/*
p4 resolve -ay //depot/some/path/*
p4 submit
This will work for any revision modifier (see p4 help revisions for alternate methods of specifying the version you want).
The rollback function is specifically designed to do this. It goes back to a certain date/time or change list # and reverts all changes in the window you give it.
Simply right click on the file in question (P4V obviously) and select rollback. It will bring up this box. Not sure how to execute from command line...Ill see if I can figure it out and add that info.

Resources