git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* On undoing a forced push
@ 2015-06-09 12:12 Duy Nguyen
  2015-06-09 13:17 ` Matthieu Moy
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Duy Nguyen @ 2015-06-09 12:12 UTC (permalink / raw)
  To: git

>From a thread on Hacker News. It seems that if a user does not have
access to the remote's reflog and accidentally forces a push to a ref,
how does he recover it? In order to force push again to revert it
back, he would need to know the remote's old SHA-1. Local reflog does
not help because remote refs are not updated during a push.

This patch prints the latest SHA-1 before the forced push in full. He
then can do

    git push <remote> +<old-sha1>:<ref>

He does not even need to have the objects that <old-sha1> refers
to. We could simply push an empty pack and the the remote will happily
accept the force, assuming garbage collection has not happened. But
that's another and a little more complex patch.

Is there any other way to undo a forced push?

-- 8< --
diff --git a/transport.c b/transport.c
index f080e93..6bd6a64 100644
--- a/transport.c
+++ b/transport.c
@@ -657,16 +657,17 @@ static void print_ok_ref_status(struct ref *ref, int porcelain)
 			"[new branch]"),
 			ref, ref->peer_ref, NULL, porcelain);
 	else {
-		char quickref[84];
+		char quickref[104];
 		char type;
 		const char *msg;
 
-		strcpy(quickref, status_abbrev(ref->old_sha1));
 		if (ref->forced_update) {
+			strcpy(quickref, sha1_to_hex(ref->old_sha1));
 			strcat(quickref, "...");
 			type = '+';
 			msg = "forced update";
 		} else {
+			strcpy(quickref, status_abbrev(ref->old_sha1));
 			strcat(quickref, "..");
 			type = ' ';
 			msg = NULL;
-- 8< --

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-06-10 12:19 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-09 12:12 On undoing a forced push Duy Nguyen
2015-06-09 13:17 ` Matthieu Moy
2015-06-09 14:06 ` Sitaram Chamarty
2015-06-09 14:25   ` Jeff King
2015-06-09 14:50     ` Sitaram Chamarty
2015-06-09 16:29   ` Johannes Schindelin
2015-06-09 16:55     ` Stefan Beller
2015-06-09 23:24     ` Duy Nguyen
2015-06-09 15:00 ` brian m. carlson
2015-06-10  2:43   ` Duy Nguyen
2015-06-10 12:18     ` brian m. carlson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).