Reverting multiple checkins in perforce - 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.

Related

How can I partially integrate a file in Perforce?

I have a file on another branch which contain a change I would like to integrate. But it contains other changes too which I don't want to integrate yet.
How can I integrate the file only partially?
I first integrated and resolved the file as usual, then I did a "p4 edit" on the file after that to remove the changes I don't want to go in yet. "p4 opened" said that the file is opened for edit, so I thought the commit will submitted as an edit not an integration. I was wrong! It still updated the integration history! So if I attempt to integrate the rest of the changes later the perforce will say "all revisions integrated", and the only way to resolve the problem is integrating by disregarding the integration history, which is painful to resolve.
How can I avoid this next time?
EDIT:
To clarify I mean there are multiple changes in a single revision of file and I want to integrate only a part of it.
(edited to reflect that the question is about partially integrating a revision, not integrating a single revision of a file)
Since a revision is the smallest atom of change in Perforce's metadata, you won't be able to record that a subset of a revision was integrated -- and since you don't want to record that the entire revision was integrated (thereby "ignoring" the rest of the revision for future integrations), rather than doing this as an integrate, I'd do it as an edit:
p4 edit target
p4 print -o theirs source#n
p4 print -o base source#n-1
p4 merge3 base theirs target > target
rm base theirs
(edit target)
p4 submit
Another option would be to initially open the file for integrate and then clear the resolve record by reverting (use the -k flag to keep local changes) and reopening for edit:
p4 integrate source#n,n target
p4 resolve
p4 revert -k target
p4 edit target
In general if there are multiple independent changes you're making to a file such that you might want to be able to cherry-pick and/or track them independently at a later date, submitting them as independent changelists will make that much much much easier. Shelving can help with this if you realize only after making a big change that it'd make more sense as a series of smaller changes -- shelve your big change, revert some parts, submit the smaller change, then unshelve the big change and continue.

How to recover a previous file version in perforce branch?

I have deleted a file from a perforce branch and I am trying to recover it. I thought of syncing to its previous version and resubmitting it. Is it the better way to do and how?
P.S: Using command line
You've got it.
p4 sync file#version
p4 add file
p4 submit
Another option (only available with server versions after ~2013):
p4 copy file#version file
p4 submit
Another other option (only available with server versions after ~2016):
p4 undo file#head
p4 submit
Any of these ways you do it, the history will record that the new version came from the older one (you'll see "file#4 add from file#2" or something along those lines), and that will do things like connect up the history in Revision Graph and allow merge operations to find a common base.

Options to preserver state of file and continue working with it

Suppose I've made some changes to a bunch of files and sent the changelist for review. The review may take up to 24 hours. During that time I might need to edit some of the files in the changelist, but when review is over, I'll need to be able to get back to the version of the file in the approved changelist. What options do I currently have to do that in Perforce?
One option that comes to mind is stashing the files and then reverting when needed, but in this way I'll lose the changes done on top of the stashed versions.
I've read about using task streams, is it something that can help me handle that situation?
I assume that the files have been shelved in the changelist for review (as opposed to emailing them off or using some other review mechanism). If so, I'd move the open files to another changelist:
p4 change
p4 reopen -c (new change) (files)
and continue working on them. The shelved versions of the files will stay in the old changelist.
If you need to go back to the old changelist, shelve your new changelist:
p4 shelve -c (new change)
and then revert your open files (they should be safe in the new shelved changelist now) and unshelve the old changelist to keep working from that point:
p4 revert (files)
p4 unshelve -s (old change)
There are lots of variations on this you could do, such as reverting and starting over from scratch rather than building your newer changes on top of the changes that are currently under review. If you do that, you'll need to merge the changes later, but Perforce will track all of that automatically and prompt you when it's time (as long as you're using Perforce commands to sync/revert/unshelve/etc -- if you start making your own backups and restoring them manually all bets are off because Perforce doesn't know what your edits are based on any more and can't guide you through the merge process intelligently).

How to sync locally modified file with server revision in 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.

re-submit changelist with new changelist number

I have submitted one file in perforce, and changes submitted by other user got reverted in merge.
Changes were not conflicting.
Is their a way, I can get that user's changes back. Means revert the revert.
Also how to submit a changelist again with new changelist number?
In order to revert a file (fall back to its previous version) you simply sync the previous version and submit that again.
For a single file the procedure goes like this:
# sync file at old revision (#3 in this example)
p4 sync //depot/file#3
# mark the file for edit
p4 edit //depot/file
# make perforce aware that something has to be merged
p4 sync //depot/file
# resolve (i.e. throw away the head revision changes and save those from #3)
p4 resolve -ay
p4 submit
If you have a changelist with several files (and not only edits, but also deletes & adds) the rollback is a bit more difficult.
See also this question.
Here's a smart script for that purpose and more info.
The 2nd part of your question I did not understand.
Submitting a change again with a new changelist number doesn't make sense, since a changelist contains diffs. If it was submitted once, then the diffs are already in the depot, so you can't submit the same diffs again.

Resources