git/list[1] front-page[2] threads[3] people[4] search[5] about
 

On undoing a forced push

From
Duy Nguyen <pclouds@gmail.com>
Date
Jun 9, 2015, 12:12 UTC
Message-ID
<20150609121221.GA14126@lanh>
>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< --
Next: Matthieu Moy
Message 1 of 11 in “On undoing a forced push”
  1. Duy NguyenJun 9, 2015
  2. Matthieu MoyJun 9, 2015
  3. Sitaram ChamartyJun 9, 2015
  4. Jeff KingJun 9, 2015
  5. Sitaram ChamartyJun 9, 2015
  6. Johannes SchindelinJun 9, 2015
  7. Stefan BellerJun 9, 2015
  8. Duy NguyenJun 9, 2015
  9. brian m. carlsonJun 9, 2015
  10. Duy NguyenJun 10, 2015
  11. brian m. carlsonJun 10, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.