Need to restore a deleted branch in Subversion

36,943

Solution 1

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...

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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"
Share:
36,943
Luke
Author by

Luke

Updated on January 24, 2020

Comments

  • Luke
    Luke over 4 years

    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?

  • Luke
    Luke over 14 years
    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"
  • John Weldon
    John Weldon over 14 years
    what was the revision # that you deleted the branch in? You have to specify that version minus 1...
  • Luke
    Luke over 14 years
    yeah, sorry, I wrote that backwards, swap the 9999 and 9998
  • Luke
    Luke over 14 years
    what do you mean, because of the username in the url?
  • John Weldon
    John Weldon over 14 years
    can you try svn cp -r 9998 svn+ssh://username@serverip/svn/branches/branch-name svn+ssh://username@serverip/svn/branches/branch-new-name ?
  • Luke
    Luke over 14 years
    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
  • leonm
    leonm over 14 years
    ok. You'll probably have to checkout /branches. Is your branches very big or is it workable?
  • zaid hussian
    zaid hussian about 13 years
    For a large projects with lots of branches, checking out /branches would be pretty costly compared to the "svn cp" that John Weldon suggested.
  • TUNAPRO1234
    TUNAPRO1234 about 11 years
    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
  • Anton Guryanov
    Anton Guryanov almost 10 years
    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).
  • Adam
    Adam almost 10 years
    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.
  • Alucard
    Alucard over 9 years
    You just saved my life.
  • John Weldon
    John Weldon over 9 years
    @Alucard I'm glad it helped :)
  • yellavon
    yellavon over 9 years
    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.
  • adamasan
    adamasan over 9 years
    Saved my day. Was inthe edge of redo about a week of work. Is great that I can keep the same name.
  • Mr. Noddy
    Mr. Noddy over 6 years
    Thanks. Steps are mentioned perfectly.
  • MarkMcDowell
    MarkMcDowell about 6 years
    Saved me from LOTS of rework due to inadvertently deleting a branch that hadn't been merged into the trunk. THANKS!
  • ToddR
    ToddR about 5 years
    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.
  • senninha
    senninha almost 5 years
    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"