How to discard all uncommitted changes in Perforce? - perforce

What is the p4 command to throw away all my uncommitted change in my Perforce workarea?
When using Git, the analogous command is: git reset --hard

If you haven't checked out the files you've edited, first do
p4 reconcile ...
to get the changes tracked by Perforce.
You don't need this if the changes you want to undo are already in any changelist.
Then, from the root of your workspace,
p4 revert ...
(... is the Perforce wildcard for everything under a given directory, not something you have to fill in yourself).
If you also want to delete untracked files from the disk, then if you're using the 2013.2 release or later, you can pass -w to p4 revert to also delete newly added files. See this answer for some background on this option.
The reconcile step would first detect these files and mark them for add, and then the revert step would unmark them for add and delete them. If -w isn't available then there's no clean way to do this and the files will be left untracked and on the disk after the revert.

p4 revert is the command you're looking for.
Remember, if you want to revert a specified changelist just, you must specify its changelist number like:
p4 revert -c changelistnumber
Or, if you are not running it from the workspace root, specify the workspace/client name, like:
p4 revert //myWorkspace/...

Related

sync cleanly to another perforce label

TL;DR: How do I sync an existing p4 workspace to a new label, so that all my previous changes are undone, and it is as good as a new sync?
Hi,
I have a perforce workspace synced to a label L1.
I added/deleted/edited a few files
I moved a few files inside a folder (mv a.txt b.txt new_folder/)
I want to cleanly sync this workspace to label L2 now.
I already removed all shelved and pending changelists, then tried
p4 reconcile -w ... && p4 revert -w ... && p4 sync ...#L2
Looks like it almost works, except that the files I moved inside a new folder do not get synced (p4 sync -f works though). But I do not want to use p4 sync -f on the entire workspace, as it would resync the unchanged files as well.
I am sorry if this question has been answered before, I am pretty new to perforce, so maybe could not find the correct terms to search for.
The ... path limits the scope of each operation to the current directory; if you ran those commands from a different directory than the one you moved the files to, that may be why they didn't get synced. I'd do:
p4 revert //...
p4 clean //...
p4 sync #L2

Deleted (not submitted) directory in Perforce: cannot revert, force-sync, reconcile or do anything else

I have deleted a directory in a 3rd-party Perforce client. It should have used p4 delete, but I cannot be sure. It was not submitted.
Now:
I don't see the deleted directory in p4v.
p4 revert fails:
p4 revert //depot/path/deleted-directory/...
//depot/path/deleted-directory/... - file(s) not opened on this client.
I cannot force-sync deleted-directory to revert the deletion:
p4 sync -f //depot/path/deleted-directory
//depot/path/deleted-directory no such file(s).
p4 sync -f //depot/path/deleted-directory/...
//depot/path/deleted-directory/file1.txt#2 - deleted as /Users/me/depot/path/deleted-directory/file1.txt
I cannot reconcile them:
p4 reconcile //depot/path/deleted-directory/...
//depot/path/deleted-directory/... - no file(s) to reconcile.
p4 opened doesn't see them:
p4 opened
File(s) not opened on this client.
How do I get this deleted directory into a changelist or at least revert it?
I tried all suggestions from "I've deleted all the files in my directory. How can I get them back?" except for checking out a new workspace, which I will eventually do.
Short answer:
p4 undo //depot/path/deleted-directory/...#head
p4 submit
That will undo the head revisions in that directory (i.e. the deletion) by creating new revisions that are copied from the prior ones.
Longer answer to explain why all the things you tried didn't work:
p4 opened and p4 revert both operate on files that are currently open. If you hadn't yet submitted, then revert would put the files back, but since this deletion was already submitted, there were no open files to revert.
When you force-synced you were just forcing a re-sync to the head revision, which is deleted (just like the files in your workspace), so no matter how hard you force the sync all you're going to get is a lot of nothing. If you had done:
p4 sync "//depot/path/deleted-directory/...#<head"
in order to sync to the revision prior to #head, that would have done the trick. Note that no -f flag is necessary. Once the files were synced to the correct revision, you would also be able to undo the delete by following that sync command with:
p4 add //depot/path/deleted-directory/...
p4 submit
Similarly, reconcile didn't do anything because the files are deleted at the head revision and deleted in your workspace, so there's nothing to reconcile. If you wanted to get really weird with it you could undo the delete via reconcile by using a combination of sync and flush to simulate the condition of having re-added new copies of the files:
p4 sync "//depot/path/deleted-directory/...#<head"
p4 flush //depot/path/deleted-directory/...
p4 reconcile //depot/path/deleted-directory/...
In addition to being unnecessarily complex, this reconcile-based solution will lose the information of which revision the re-added files came from (thanks to the p4 flush), so I would not recommend it. Go with undo and if you're on an old server version that doesn't support that do the normal sync/add thing.

How to rollback in perforce(p4) to a changelist using a command line(Not Gui)

I am a perforce beginner user.
I am writing a script that adds, deletes and edits files and revert all changes at the end.
I tried to do the following to revert all changes:
p4 sync #oldRevision
p4 add
p4 sync
p4 resolve -ay
p4 submit
and this revert all changes and add the deleted files but it does not remove the new added files.
How can I remove the new added file?
I noticed that when I run "p4 sync #oldRevision" the new created file which was not exist in this release are mapped from the latest release and may be this is the issue
Thanks in advance
p4 undo "#>oldRevision"
Or if oldRevision is a changelist, add 1 and use a range. I.e. these two commands are the same:
p4 undo "#>1000" # undo all changes after 1000
p4 undo #1001,#now # undo all changes from 1001 to now
https://www.perforce.com/perforce/r16.2/manuals/cmdref/p4_undo.html
https://www.perforce.com/perforce/r16.2/manuals/cmdref/filespecs.html#filespecs.synopsis.using_revision_ranges

How to switch to a change-list when using perforce command line (p4)

I opened a bunch of files using p4 edit in my workspace and after making some changes on each of them, I decided to put them up for review so I staged them using p4 change and I got a number to mark the changelist. Once I did that I am not able to see the actual changes made in the workspace. The files have gone to a version prior to my p4 edit.
How do I revert my workspace to a changes pointing to a change-list?
p4 undo -c 403767
p4 undo #403767
p4 switch #403767
p4 revert -c 403767
None of the above seems to work. Also when I do a p4 opened -u <author-name> I have a string that says integrate change 403767 (text) for all the files listed.
The integrate change 403767 indicates that these files are open for integrate, so these can't be the files that you opened with p4 edit.
Files can be opened for integrate by the p4 integrate, p4 copy, p4 merge, or p4 undo command.
Further, if a file is listed by p4 opened, its current working state is already in your workspace. If you had opened the file for edit and neither reverted nor submitted it, p4 opened would say edit change 403767 and your edits would be in your workspace.
I will hazard a guess that what you are actually trying to do is integrate changes from another branch, and you're confused because those changes do not yet appear in your workspace. Run:
p4 resolve -am
p4 resolve
That might get things into the state you're expecting.

How to do I get files I deleted locally but didn't commit the changes?

I have deleted some files/folders in my local machine but I did not commit the changes to repository.
How to get the deleted files back?
I am not getting the deleted files after performing the p4 sync -f. Why I am not getting the deleted files after the force sync?
If you haven't reverted the changes Perforce will still think you want to delete those files so won't restore them.
You need to go to the pending changelist and do a "revert" on those files to get them back.
Use the "p4 revert" command:
p4 revert -c default //...
This "revert[s] every file open in the default changelist to its pre-opened state."
Source
If you don't want to revert all the files, move the files you want to revert to a separate changelist and use the "-c" option.
If you only have a few files to revert you can specify the name of the file:
p4 revert lib/libopenssl.a include/openssl.h
for example, reverts exactly those two files (thanks to Bryan).

Resources