+else
+ git-read-tree --reset "$rev" || exit
+fi
+
+# Any resets update HEAD to the head being switched to.
+if orig=$(git-rev-parse --verify HEAD 2>/dev/null)
+then
+ echo "$orig" >"$GIT_DIR/ORIG_HEAD"
+else
+ rm -f "$GIT_DIR/ORIG_HEAD"
+fi
+echo "$rev" >"$GIT_DIR/HEAD"
+
+case "$reset_type" in
+--hard )
+ # Hard reset matches the working tree to that of the tree
+ # being switched to.
+ git-checkout-cache -f -u -q -a
+ git-ls-files --cached -z |
+ perl -e '
+ use strict;
+ my (%keep, $fh);
+ $/ = "\0";
+ while (<STDIN>) {
+ chomp;
+ $keep{$_} = 1;
+ }
+ open $fh, "<", $ARGV[0]
+ or die "cannot open $ARGV[0]";
+ while (<$fh>) {
+ chomp;
+ if (! exists $keep{$_}) {
+ print "$_\0";
+ }
+ }
+ ' $tmp-exists | xargs -0 rm -v -f --
+ ;;
+--soft )
+ ;; # Nothing else to do
+--mixed )
+ # Report what has not been updated.
+ git-update-cache --refresh
+ ;;
+esac
+