diff --git a/hggit/git_handler.py b/hggit/git_handler.py
index 338e4e5ba6dcf2f9d90ac895dac3a8ff1432d495_aGdnaXQvZ2l0X2hhbmRsZXIucHk=..f405735327130ba6d9c0f2e192812f3417e7b709_aGdnaXQvZ2l0X2hhbmRsZXIucHk= 100644
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -1403,7 +1403,10 @@
 
             suffix = self.branch_bookmark_suffix or b''
             changes = []
-            for head, sha in compat.iteritems(heads):
+
+            for head in sorted(heads):
+                sha = heads[head]
+
                 # refs contains all the refs in the server, not just
                 # the ones we are pulling
                 hgsha = self.map_hg_get(sha)
@@ -1434,6 +1437,10 @@
         for t in list(remote_refs):
             if t.startswith(remote_name + b'/'):
                 del remote_refs[t]
+
+                if b'refs/heads/' + t[len(remote_name) + 1:] not in refs:
+                    os.unlink(os.path.join(self.gitdir, b'refs/remotes', t))
+
         for ref_name, sha in compat.iteritems(refs):
             if ref_name.startswith(b'refs/heads'):
                 hgsha = self.map_hg_get(sha)
diff --git a/tests/test-pull.t b/tests/test-pull.t
index 338e4e5ba6dcf2f9d90ac895dac3a8ff1432d495_dGVzdHMvdGVzdC1wdWxsLnQ=..f405735327130ba6d9c0f2e192812f3417e7b709_dGVzdHMvdGVzdC1wdWxsLnQ= 100644
--- a/tests/test-pull.t
+++ b/tests/test-pull.t
@@ -167,7 +167,6 @@
   | |
   o |  changeset:   1:7fe02317c63d
   |/   bookmark:    beta
-  |    tag:         default/beta
   |    tag:         t_beta
   |    user:        test <test@example.org>
   |    date:        Mon Jan 01 00:00:11 2007 +0000
@@ -244,7 +243,6 @@
   | |
   o |  changeset:   1:7fe02317c63d
   |/   bookmark:    beta
-  |    tag:         default/beta
   |    tag:         t_beta
   |    user:        test <test@example.org>
   |    date:        Mon Jan 01 00:00:11 2007 +0000