Need to restore a deleted branch in Subversion
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:
- In the repo browser, navigate to the parent folder of the folder you deleted (e.g. "branches").
- Right click on the folder and do a "Show Log."
- Find the revision where you deleted the specific branch folder.
- Select the revision immediately before that revision.
- 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.
- Find the branch folder that you deleted, select, right-click, and choose "Copy to..."
- 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"
Luke
Updated on January 24, 2020Comments
-
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 over 14 yearsdidn'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 over 14 yearswhat was the revision # that you deleted the branch in? You have to specify that version minus 1...
-
Luke over 14 yearsyeah, sorry, I wrote that backwards, swap the 9999 and 9998
-
Luke over 14 yearswhat do you mean, because of the username in the url?
-
John Weldon over 14 yearscan you try svn cp -r 9998 svn+ssh://username@serverip/svn/branches/branch-name svn+ssh://username@serverip/svn/branches/branch-new-name ?
-
Luke over 14 yearsthis 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 over 14 yearsok. You'll probably have to checkout /branches. Is your branches very big or is it workable?
-
zaid hussian about 13 yearsFor a large projects with lots of branches, checking out /branches would be pretty costly compared to the "svn cp" that John Weldon suggested.
-
TUNAPRO1234 about 11 yearsNote 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 almost 10 yearsThis 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 almost 10 yearsYou 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 over 9 yearsYou just saved my life.
-
John Weldon over 9 years@Alucard I'm glad it helped :)
-
yellavon over 9 yearsFYI, 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 over 9 yearsSaved my day. Was inthe edge of redo about a week of work. Is great that I can keep the same name.
-
Mr. Noddy over 6 yearsThanks. Steps are mentioned perfectly.
-
MarkMcDowell about 6 yearsSaved me from LOTS of rework due to inadvertently deleting a branch that hadn't been merged into the trunk. THANKS!
-
ToddR about 5 yearsThis 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 almost 5 yearsif 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"