170

If I delete a file in Subversion, how can I look at it's history and contents? If I try to do svn cat or svn log on a nonexistent file, it complains that the file doesn't exist.

Also, if I wanted to resurrect the file, should I just svn add it back?

(I asked specifically about Subversion, but I'd also like to hear about how Bazaar, Mercurial, and Git handle this case, too.)

Derrick Rice
  • 155
  • 7
Benjamin Peterson
  • 19,297
  • 6
  • 32
  • 39

17 Answers17

158

When you want to look at old files you really should know the difference between:

svn cat http://server/svn/project/file -r 1234

and

svn cat http://server/svn/project/file@1234

The first version looks at the path that is now available as http://server/svn/project/file and retrieves that file as it was in revision 1234. (So this syntax does not work after a file delete).

The second syntax gets the file that was available as http://server/svn/project/file in revision 1234. So this syntax DOES work on deleted files.

You can even combine these methods to retrieve a file that was available in revision 2345 as http://server/svn/project/file but with the contents as it had in 1234 with:

svn cat http://server/svn/project/file@2345 -r 1234
Bert Huijben
  • 19,525
  • 4
  • 57
  • 73
  • 7
    Gah, thanks! The current top response in this thread does not mention this, this is great! – Keith Palmer Jr. Jan 08 '10 at 15:53
  • This still failed for me unless I used absolute paths, since my local svn client was giving an error when unable to resolve `./local/file` when the `./local` directory did not exist. This might not be a problem for newer versions of SVN. – Derrick Rice Jun 06 '13 at 18:00
  • 2
    @DerrickRice: In that case, the `^` notation comes handy: it refers to the repository root, so you can say `svn cat ^/local/file@REV` (depending on the distance between the repository root and URL). – musiphil Sep 11 '13 at 22:49
  • This works great in principle. For folders I receive the following: `svn: E200009: Could not cat all targets because some targets are directories` – Barney Apr 23 '14 at 17:07
  • This is the best answer. Has the highest votes, too. – Felipe Alvarez May 19 '14 at 01:21
  • This is the answer you are looking for. Not sure how the accepted answer achieved that many votes or got accepted because it doesn't work as far as I can tell. – wytten May 20 '14 at 19:44
  • for rev in {900..600}; do if svn cat http://server/repo/file.ext@$rev > /dev/null 2>&1; then echo "Last seen at revision: $rev"; break; fi; done – Kevin Mar 15 '17 at 19:36
99

First, find the revision number where the file got deleted:

svn log -v > log.txt

Then look in log.txt (not an SVN guru, so I don't know a better way) for a line with

D <deleted file>

and see which revision that was. Then, as in the other answers, resurrect the file using the previous revision.

mjy
  • 2,727
  • 2
  • 20
  • 22
  • 22
    svn log -v | grep D "file.name" – abatishchev Dec 30 '08 at 21:19
  • 20
    +1 for being the first person to correctly answer the question. You can't look at the contents if you don't know the revision before it was deleted. – Cerin Jul 27 '10 at 17:58
  • 8
    @abatishchev this obtains the list of deleted files, but discards the revision info, so it's not useful. Also it's slow if you're working with a large/old repository with much change history. – tchen Oct 28 '11 at 16:37
  • 4
    Good, great with @abatishchev's improvement. tchen: easily fixed using a -B50 or so argument to grep, see my answer. – Jonas Byström Sep 06 '12 at 07:36
  • 2
    Another good way to limit the svn log -v output for very large/old repositories is the -l option. So you could use svn log -v -l 100 | grep D "file.name" – mindmatters Oct 09 '13 at 15:52
86

To get the log of a deleted file, use

svn log -r lastrevisionthefileexisted

If you want to resurrect the file and keep its version history, use

svn copy url/of/file@lastrevisionthefileexisted -r lastrevisionthefileexisted path/to/workingcopy/file

If you just want the file content but unversioned (e.g., for a quick inspection), use

svn cat url/of/file@lastrevisionthefileexisted -r latrevisionthefileexisted > file

In any case, DO NOT use 'svn up' to get a deleted file back!

Stefan
  • 43,293
  • 10
  • 75
  • 117
  • 3
    You can also ressurrect the file by doing a reverse merge of the revision in which you deleted it. This is the procedure recommended in the SVN docs. As for using "svn up", it's not so much a matter of "don't do it" as it is "it will not do what you want it to do". – rmeador Dec 30 '08 at 20:18
  • 5
    How can I see the file's whole history, though? – Benjamin Peterson Dec 30 '08 at 20:41
  • I'd edit the answer if I could, but since I can't, the way to get the whole log is to use a revision range of the form 1:lastrevisionfileexisted (or perhaps it starts at zero... can't remember) – rmeador Dec 30 '08 at 21:15
  • 71
    Simple: show the log for a parent folder with the '-v' switch: you'll get for every entry a list of changed paths. Find the one with a 'D' in front and the name of your deleted file. That's the revision where the file got deleted. – Stefan Mar 27 '09 at 19:58
  • 9
    This doesn't seem to work for deleted files. If I try this, I get this error message: svn cat [url]/trunk/include/syeka/poster_funk.incl.php -r 50 > out.txt svn: '/admintools/!svn/bc/131/trunk/include/syeka/poster_funk.incl.php' path not found See @Bert Huijben's response further down this thread for a working solution. – Keith Palmer Jr. Jan 08 '10 at 15:55
  • 5
    If I have a repo with 100,000 commits, "lastrevisionthefileexisted" is not easy to find! – Jon Watte Jul 06 '16 at 19:55
  • If you know the name of the file, or part of it, but don't remember when you deleted it you could use this quick script I threw together a few years back: https://github.com/inquam/svn-find-deleted-file – inquam May 26 '17 at 20:15
22

It's nothing particularly special in git. If you know the name of the file, you can find out the change that removed it with log:

git log -n 1 -- filename

Then you can use that commit to get the file as it existed before the deletion.

git checkout [last_revision]^ filename

Example:

dhcp-120:/tmp/slosh 587% ls -l slosh.tac
ls: slosh.tac: No such file or directory
dhcp-120:/tmp/slosh 588% git log -n 1 -- slosh.tac
commit 8d4a1f1a94e4aa37c1cb9d329a140d08eec1b587
Author: Dustin Sallings <dustin@spy.net>
Date:   Mon Dec 15 11:25:00 2008 -0800

    Get rid of a .conf and replace it with .tac.
dhcp-120:/tmp/slosh 589% git checkout 8d4a1f^ slosh.tac
dhcp-120:/tmp/slosh 590% ll slosh.tac
-rw-------  1 dustin  wheel  822 Dec 30 12:52 slosh.tac

Note that this does not actually put the file back in revision control. It simply drops the file as it existed in its final state into the current location. You can then add it or just inspect it or whatever from that point.

Dustin
  • 89,080
  • 21
  • 111
  • 133
  • 8
    Wonderful answer. Only problem is that the question is about svn! – JohnK Jul 13 '17 at 21:13
  • As the post makes no attempt to answer the original question which is explicitly about Subversion, **consider downvoting it** in order to give it a chance to be removed. – Arseni Mourzenko Apr 12 '21 at 19:47
17

A solution using only the GUI:

If you know the name of the file, but don't know its last revision number or even its path:

  1. From Repo Browser do a "Show log" on the root
  2. Hit "Show All" (at the bottom of the log dialog)
  3. Type the filename into the Filter textbox (at the top of the log dialog)

This will then show only those revisions where the file was added/modified/deleted. This is your history of the file.

Note that if the file was deleted by deleting one of its parent folders, it won't have a 'deleted' entry in the log (and so mjy's solution won't work). In this case, its most recent entry in the filtered log will correspond to its contents at deletion.

Mark Foreman
  • 2,190
  • 18
  • 16
  • Brute force is not always the shit. Especially not on big repos. – Jonas Byström Sep 06 '12 at 07:32
  • 1
    +1 for a UI only solution. Command line is great, and all, but it's not always the best answer without exception. Especially when you're working in an environment you don't control, and don't have easy command line access to SVN. – Mir Oct 17 '12 at 22:28
  • 3
    Please note that the above answer is meant for the TortoiseSVN GUI. – Georg Muehlenberg Nov 26 '19 at 08:27
13
svn log -v | grep -B50 YourDeletedFileName

Will get you the path and revision. In git (also checks for renames):

git log --diff-filter=DR --name-only | grep -B50 YourDeletedFileName
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
  • What does the -B50 do? I can get a list of files using svn log and grep quite easily using the tips here, but I can't seem to get the revision numbers displayed at all easily, being as they are displayed on a different line. I tried the B50 thing and it didn't seem to work that amazingly for me. – cedd Jul 30 '15 at 14:12
  • It outputs the mathced line and the 50 lines above that in case anybody else is reading this. – cedd Jul 30 '15 at 14:14
10

If you don't know the path to the deleted file, turns out you can search for it in the otherwise all-too-heavy svn log command:

svn log --search <deleted_file_or_pattern> -v

The command is probably hammering the server just as much as it would without the search option, but at least the rest of involved resources (including your eyeballs) would be kinda relieved, since that will tell you in which revision that file was deleted. Then, you can follow the other tips (mainly using the same svn log command, but already on a defined path).

JMB
  • 137
  • 1
  • 7
9

Use this command:

svn log -v | awk '/^r[0-9]+/ { rev = $1; }; / D .*filename_escaped_for_regex/ { print rev" "$2; };'

This will list all revisions that ever deleted any files matching the pattern. That is, if you're searching for file README, then all of /src/README, /src/README.first, and /some/deeply/hidden/directory/READMENOT will be found and listed.

If your filename contains slashes (path), dots, or other special regex characters, don't forget to escape them to avoid mismatching or errors.

Alexander Amelkin
  • 759
  • 1
  • 9
  • 15
8

In addition to Dustin's answer, if you just want to examine the contents, and not check it out, in his example you can do:

$ git show 8d4a1f^:slosh.tac

the : separates a revision and a path in that revision, effectively asking for a specific path at a specific revision.

Pieter
  • 4,010
  • 1
  • 18
  • 15
5

The poster has actually asked 3 questions here:

  1. How do I look at the history of a deleted file in Subversion?
  2. How do I look at the contents of a deleted file in Subversion?
  3. How do I resurrect a deleted file in Subversion?

All the answers I see here are for questions 2 and 3.

The answer to question 1 is:

svn log http://server/svn/project/file@1234

You still need to get the revision number for when the file last existed, which is clearly answered by others here.

dekeguard
  • 298
  • 4
  • 7
4

Ah, since I am learning to use Bazaar, it is something I tried. Without success, it appears you cannot log and annotate removed files currently... :-(

Tried:

> bzr log -r 3 Stuff/ErrorParser.hta
bzr: ERROR: Path does not have any revision history: Stuff/ErrorParser.hta

but curiously (and fortunately) I can do:

> bzr cat -r 3 Stuff/ErrorParser.hta

and:

> bzr diff -r 2..3 Stuff/ErrorParser.hta

and as suggested in the bug above:

> bzr log -v | grep -B 1 ErrorParser

(adjust -B (--before-context) parameter as needed).

PhiLho
  • 40,535
  • 6
  • 96
  • 134
2

Assume your file was named as ~/src/a/b/c/deleted.file

cd ~/src/a/b/c  # to the directory where you do the svn rm or svn mv command
#cd ~/src   # if you forget the correct directory, just to the root of repository
svn log -v | grep -w -B 9 deleted.file | head  # head show first 10 lines

sample output, found it at r90440

...
r90440 | user | 2017-02-03 11:55:09 +0800 (Fri, 03 Feb 2017) | 4 lines
Changed paths:
  M /src/a/b/c/foo
  M /src/a/b/c/bar
  D /src/a/b/c/deleted.file

copy it back to previous version(90439=90440-1)

svn cp URL_of_deleted.file@90439 .
Daniel YC Lin
  • 15,050
  • 18
  • 63
  • 96
1

I wanted an answer, myself. Try the following to output only deletes from svn log.

svn log --stop-on-copy --verbose [--limit <limit>] <repo Url> | \
awk '{ if ($0 ~ /^r[0-9]+/) rev = $0 }
  { if ($0 ~ /^ D /) { if (rev != "") { print rev; rev = "" }; print $0 } }'

This filters the log output through awk. awk buffers each revision line it finds, outputting it only when a delete record is found. Each revision is only output once, so multiple deletes in a revision are grouped together (as in standard svn log output).

You can specify a --limit to reduce the amount of records returned. You may also remove the --stop-on-copy, as needed.

I know there are complaints about the efficiency of parsing the whole log. I think this is a better solution than grep and its "cast a wide net" -B option. I don't know if it is more efficient, but I can't think of an alternative to svn log. It's similar to @Alexander Amelkin's answer, but doesn't need a specific name. It's also my first awk script, so it might be unconventional.

nshew13
  • 3,052
  • 5
  • 25
  • 39
1

You would need to specify a revision.

svn log -r <revision> <deleted file>
Jack M.
  • 30,350
  • 7
  • 55
  • 67
  • 1
    This gives an error. Example: svn log -r 37428 http://svn.example.com/deletedfile.java svn: '/!svn/bc/98571/deletedfile.java' path not found – Jeremy Mar 27 '09 at 18:24
  • Are you sure it existed at that revision? You have to specify a revision where the file actually existed. – Jack M. Mar 27 '09 at 20:16
  • See the answer by Bert Huijben for the weird different between -r37428 and adding @37428 to the SVN URL. – dubek Jan 20 '11 at 06:31
1

If you're wanting to look at the history of a file prior to it being renamed, then as mentioned in a comment here you can use

git log --follow -- current_file_name
Community
  • 1
  • 1
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
0

You can find the last revision which provides the file using binary search. I have created a simple /bin/bash script for this:

function svnFindLast(){
 # The URL of the file to be found
 local URL="$1"
 # The SVN revision number which the file appears in (any rev where the file DOES exist)
 local r="$2"
 local R
 for i in $(seq 1 "${#URL}")
  do
   echo "checkingURL:'${URL:0:$i}'" >&2
   R="$(svn info --show-item revision "${URL:0:$i}" 2>/dev/null)"
   echo "R=$R" >&2
   [ -z "$R" ] || break
 done
 [ "$R" ] || {
  echo "It seems '$URL' is not in a valid SVN repository!" >&2
  return -1
 }
 while [ "$r" -ne "$R" -a "$(($r + 1))" -ne "$R" ]
 do
  T="$(($(($R + $r)) / 2))"
  if svn log "${URL}@${T}" >/dev/null 2>&1
   then
    r="$T"
    echo "r=$r" >&2
   else
    R="$T"
    echo "R=$R" >&2
  fi
 done
 echo "$r"
}
Sebo.PL
  • 51
  • 4
-1

I wrote a php script that copies the svn log of all my repositories into a mysql database. I can now do full text searches on my comments or names of files.

thinsoldier
  • 151
  • 2
  • 8