69

I have two working copies of a Subversion repository, one of the trunk, and one of a branch I created.

I accidentally deleted the branch in a repository browser. How do I restore the branch? Is there a way to undo the most recent commit for the whole repository?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke
  • 1,345
  • 2
  • 10
  • 10

7 Answers7

102

Here is a solution if you are using TortoiseSVN:

  1. In the repo browser, navigate to the parent folder of the folder you deleted (e.g. "branches").
  2. Right click on the folder and do a "Show Log."
  3. Find the revision where you deleted the specific branch folder.
  4. Select the revision immediately before that revision.
  5. Right click and choose "Browse Repository." You are now looking at the state of the repository at the point in time right before you deleted the branch.
  6. Find the branch folder that you deleted, select, right-click, and choose "Copy to..."
  7. You can now copy the deleted folder to either a new name or even the same name.
Brian Neal
  • 31,821
  • 7
  • 55
  • 59
  • You can then go to the renamed branch in the latest/head repo browser and copy back to the original branch name. The log history for the branch will then show a detour via the "restored" branch but will work. – Adam Jul 17 '14 at 12:07
  • FYI, a nice additional feature is that the revision history for the branch is restored as well. You just right click on the restored branch and "Show Log". Then if you uncheck "Stop on copy/rename", it will show you the entire branch history. – yellavon Sep 02 '14 at 14:07
  • Saved my day. Was inthe edge of redo about a week of work. Is great that I can keep the same name. – adamasan Sep 24 '14 at 12:34
  • Thanks. Steps are mentioned perfectly. – Mr. Noddy Nov 27 '17 at 07:01
  • Saved me from LOTS of rework due to inadvertently deleting a branch that hadn't been merged into the trunk. THANKS! – MarkMcDowell Mar 02 '18 at 16:52
83

Use:

svn cp [path to deleted branch]@[revision before delete] [new path]

For example:

svn cp svn://myrepo.com/svn/branches/2.0.5@1993 \
       svn://myrepo.com/svn/branches/2.0.5_restored

Where 1993 is the revision before the delete...

Here is some good documentation...

There must be some way of escaping the @ symbol in the username...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • didn't seem to work, I am using svn+ssh because it's on a local server, so I tried svn cp svn+ssh://username@serverip/svn/branches/branch-name@9999 svn:ssh://username@serverip/svn/branches/branch-name and I got "Path 'svn+ssh://username@serverip/svn/branches/branch@9999' does not exist in revision 9998" – Luke Oct 14 '09 at 19:58
  • what was the revision # that you deleted the branch in? You have to specify that version minus 1... – John Weldon Oct 14 '09 at 20:02
  • yeah, sorry, I wrote that backwards, swap the 9999 and 9998 – Luke Oct 14 '09 at 20:05
  • what do you mean, because of the username in the url? – Luke Oct 14 '09 at 20:12
  • 1
    can you try svn cp -r 9998 svn+ssh://username@serverip/svn/branches/branch-name svn+ssh://username@serverip/svn/branches/branch-new-name ? – John Weldon Oct 14 '09 at 20:13
  • Note this works for items that were deleted and then need to be brought back in. For example, if I deleted svn://myrepo.com/svn/trunk/main/widget (the widget directory and everything underneath), but then decided that was not the correct thing to do, widget can be added back with the same syntax: svn cp svn://myrepo.com/svn/trunk/main/widget@[revision before delete] svn://myrepo.com/svn/trunk/main/widget followed by svn commit – natersoz May 15 '13 at 22:42
  • 1
    if it doesn't work, add -m "commit message" like this: svn cp svn://myrepo.com/svn/branches/2.0.5@1993 svn://myrepo.com/svn/branches/2.0.5_restored -m "commit message" – senninha Aug 02 '19 at 04:15
4

Assuming your last revision was 108:

svn merge --revision 108:107
svn diff 
svn commit -m "Reverted revision 108"

You can also add your source URL to the merge:

svn merge --revision 108:107 http://svn/repo/

Elsewhere on Stack Overflow: Undoing a commit in TortoiseSVN

Community
  • 1
  • 1
leonm
  • 6,454
  • 30
  • 38
  • this doesn't seem to work either. I get the error that the path /branches/branch-name doesn't exist in revision 108, where 108 is the revision after I deleted the branch – Luke Oct 14 '09 at 20:17
  • 1
    ok. You'll probably have to checkout /branches. Is your branches very big or is it workable? – leonm Oct 14 '09 at 20:57
  • 1
    For a large projects with lots of branches, checking out /branches would be pretty costly compared to the "svn cp" that John Weldon suggested. – Alan Krueger Mar 23 '11 at 21:53
3

I ran into this problem, but the above command didn't work for me. What did was much easier. I checked out the branch at one revision before I removed it.

The revision that was removed was 9331. I checked it out at 9330:

svn co https://svn.acme.com/svn/giantFlySwatterTargetingSystem/branches/bug1234@9330 restored

That was the easy solution I wanted.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alan B. Dee
  • 5,490
  • 4
  • 34
  • 29
  • 2
    This way you'll get your working copy in the desired state, but the repository will stay the same, so other developers will not see the deleted branch (which is likely not what you wanted to achieve). – Anton Guryanov Jul 03 '14 at 13:38
1

This worked for me,

svn cp --username your_user_name https://path_to_your_repo/branches/deleted_branch_name@last_revision_before_deletion https://path_to_your_repo/branches/new_name_for_branch

Sibin John Mattappallil
  • 1,739
  • 4
  • 23
  • 37
1

Alternatively, if it was a single commit that deleted the branch:

Revert the commit and then commit

svn merge -c -REV .
svn commit -m "Reverting deletion of branch XYZ"
BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • 1
    This assumes you have the parent checked out as your working copy. If the parent contains many files/folders, this can be very time consuming and take up a lot of disk space. However, if you check out the parent using `--depth immediates` and then run the specified merge command with `--depth infinity` you can avoid checking out the whole parent. – ToddR Apr 25 '19 at 16:30
0

I ran into the same problem and solved it this way in SmartSVN (Enterprise 6.0.2):

  • Open a view on the trunk/HEAD
  • Call menu Repository / Open in Repository Browser (the deleted branch is absent from the branches folder)
  • In the repository browser, call menu Repository / Show Revision...
  • Select the revision just before the deletion (the deleted branch reappears in the branches folder)
  • Right-click on the deleted branch, select Copy in the menu and enter the same path for the destination