examining history of deleted file

SvnVersion ControlBazaar

Svn Problem Overview


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

Svn Solutions


Solution 1 - Svn

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

Solution 2 - Svn

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.

Solution 3 - Svn

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!

Solution 4 - Svn

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.

Solution 5 - Svn

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.

Solution 6 - Svn

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

Solution 7 - Svn

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.

Solution 8 - Svn

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

Solution 9 - Svn

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.

Solution 10 - Svn

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.

Solution 11 - Svn

Ah, since I am learning to use Bazaar, it is something I tried. Without success, it appears you cannot [log and annotate removed files](https://bugs.launchpad.net/bzr/+bug/255687 "log and annotate should work on 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).

Solution 12 - Svn

You would need to specify a revision.

svn log -r <revision> <deleted file>

Solution 13 - Svn

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

Solution 14 - Svn

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.

Solution 15 - Svn

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 .

Solution 16 - Svn

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"
}

Solution 17 - Svn

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenjamin PetersonView Question on Stackoverflow
Solution 1 - SvnBert HuijbenView Answer on Stackoverflow
Solution 2 - SvnmjyView Answer on Stackoverflow
Solution 3 - SvnStefanView Answer on Stackoverflow
Solution 4 - SvnDustinView Answer on Stackoverflow
Solution 5 - SvnMark ForemanView Answer on Stackoverflow
Solution 6 - SvnJonas ByströmView Answer on Stackoverflow
Solution 7 - SvnAlexander AmelkinView Answer on Stackoverflow
Solution 8 - SvnJMBView Answer on Stackoverflow
Solution 9 - SvnPieterView Answer on Stackoverflow
Solution 10 - SvndekeguardView Answer on Stackoverflow
Solution 11 - SvnPhiLhoView Answer on Stackoverflow
Solution 12 - SvnJack M.View Answer on Stackoverflow
Solution 13 - SvnAndrew GrimmView Answer on Stackoverflow
Solution 14 - Svnnshew13View Answer on Stackoverflow
Solution 15 - SvnDaniel YC LinView Answer on Stackoverflow
Solution 16 - SvnSebo.PLView Answer on Stackoverflow
Solution 17 - SvnthinsoldierView Answer on Stackoverflow