🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #4131

Re: Prevent git reset --hard from losing data

#4131
From: "D. Ben Knoble"
Date: Mon, 30 Nov 2026 14:54
64 lines
14006 bytes
Hi Steven, forgive the incoming question if it appears rude (I am
genuinely curious):

On Mon, Nov 23, 2026 at 4:00 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Fri, 20 Nov 2026 22:09:32 +0100
> Mirko Faina <mroik@delayed.space> wrote:
>
> > You could just reset without the --hard option and restore what you
> > don't want to keep. Otherwise there's the --keep option that aborts the
> > reset if there's local changes, but you'd have to remember to type it
> > out every time.
>
> I just tried it without the --hard and it reminded me why I never use that.
>
> I just wanted to update one of my branches to one of my devel branches and
> did a git reset without the hard and at the end it gives me:

[a large diff between HEAD and working tree]

Here a simple "git restore ." would restore the working tree to match
the index (which should be equivalent to HEAD), right?

I confess I'm very confused about why you use reset to update a branch
in the first place: you also wrote

> The reason I use git reset --hard is because I work on several topic
> branches and create development ones all the time. Sometimes I add commits
> across branches. I'll create a temp branch and work on it until I like it
> and then copy the sha1 of that branch, checkout the branch I want to use,
> and do a "git reset --hard sha1" to convert the actual branch to the temp
> one.

It seems to me you could instead do

    git switch -c temp <some-useful-base>
    … work, commit …
    git switch branch-i-care-about
    git merge temp

and edit the commit message as needed, or replace the merge with "git
cherry-pick ..temp" (or rebase "temp" before merging) if you prefer a
(simulated) fast-forward.

On the other hand, you could also do "git branch -f
branch-i-care-about temp", with the same kinds of "force flags can
lose work" caveats, though at least in that case you only risk losing
the commits referred to by branch-i-care-about@{1} when the GC runs
(as opposed to potentially losing uncommitted work with reset).

So, have you not considered those options? Or have you, and what about
them is unsuitable compared to git-reset? (Yes, I know the document
point of git-reset is to "Set 'HEAD' or the index to a known state,"
so it's not totally out of the ordinary to use it to set a branch
[HEAD] to another branch [known state], but I find the pattern a bit
odd.)

-- 
D. Ben Knoble
Technical Details
Message-ID: <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com>
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76689-linux-kernel-git=dewy.ru
References: <20261120145444.13061eb7@gandalf.local.home> <awC0MJDYS88yEtgm@exploit> <20261123160009.61bf0bce@gandalf.local.home>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76689-linux-kernel-git=dewy.ru
From: ben.knoble@gmail.com ("D. Ben Knoble")
Newsgroups: linux.kernel.git
Subject: Re: Prevent git reset --hard from losing data
Date: Mon, 30 Nov 2026 09:54:55 -0500
Organization: Usenet Network
Message-ID: <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com>
References: <20261120145444.13061eb7@gandalf.local.home> <awC0MJDYS88yEtgm@exploit> <20261123160009.61bf0bce@gandalf.local.home>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network; logging-data="1449989"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:WhM9j2otaAthcvvMmINMdqhL2UM= sha256:3M9EmZYz/glrCTKOIG9sFH2FWyUE8TEz1AIZUXlUqIs= sha1:SL5jsoL0W4A4CRfvNfsWhw2LGio= sha256:CRC5nzNGy4SeVFjhuqfafEi/JpKigTeTOvOXPwSN/KY=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 8FC8A125FF1; Mon, 30 Nov 2026 17:55:28 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.949
X-Spam-Status: No, score=-1.949 tagged_above=-9999 required=6.31 tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id pcUUdlPAj-eu; Mon, 30 Nov 2026 17:55:25 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=git+bounces-76689-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 569A2125FEE; Mon, 30 Nov 2026 17:55:13 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 7A27F2332D; Mon, 30 Nov 2026 14:55:11 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796050509; b=DlyOdg3AOespN5vrFDH+avsGXuEXUKPSglyml1sJ4yQpS72JYKcWxNpKXofTWgc2OQ+X 4b3Cjw1eq7qW1INfHCFSWwzLY9K7glqkPdkxuvIM9qkIaSM34JXonIevfN5y0F/6UWrYe x4fiibRcfHTAfrbuPXL25pku6b2ZhTkckoOzRAqdwf/4jX5jByPupZusakDJfkTCuMz6V vjXUBgE9/JhyC1odotu4xQ9YfSO31hL57L8Bdarv/lrPXieWl5XMo2Z8sxaaDrePFKTm2 N2SV+GvVFWlz0BxTKCkSNxtXq4K2iRS58XtZb2Ome2HT74NfRG1LzL9OBziZ74V2lyg==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796050509; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature: X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State: X-Gm-Gg:X-Received:X-Mailing-List:List-Id:List-Post:List-Help: List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version: References:In-Reply-To:From:Date:X-Gm-Features:Message-ID:Subject:To: Cc:Content-Type:Content-Transfer-Encoding; bh=XtonR6A2IClIMTzj6FeeA7NI+ZtICXC3ZfmYDFbM2lo=; b=tQdDT6yFnk2qYnx3GvhR4kujyV1vBfpY0mNIKg2PizZeKD/hZFSBLXjYOEm4rXuz5Pdg r+4cS3O5QPcxYWPhWBvtbirKrt8yzVJHQxVcAcss/2xVAZMnkJLdSvc68owt1NmBNpUMh Jkx5XB6LRHMFBjJvoiGU3x2i14s/AJOhAMvH012G+Tfp/lSNI7dSqRGZl9wJsMj6VJ+Jv oRR8sQTgmnqttuJTr/HxJtoudgVGtIjh9lsPNOH2sBtoeROJvKaPIqsrDxAKyZJKuNKmu b3A/3nWvuZBCuB09vsVdYG/eggPrNck3MaZlcC0FW8JDylQ8YXqSkePPDZ9Bb+7qtnA==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=mU5OrH8F; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0D1F606A32C; Mon, 30 Nov 2026 14:55:08 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=mU5OrH8F
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=209.85.214.172
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796050508; b=xMAB+SQt0UJ/T/EFcpRXiJuGqPNfcdnsYJ8uivHbCdFFKEWPYf5Wg9lH26uLHIzA4XLg /QjD+fD4svdLWsgBJ6/d2lYU2Rn4W2spHqBP3/W8IQwM+kEBF/MG5cIFRD53WmALScQj4 H9OQgFHJXbUnxlOfokoU+p+Nx6b+dMk3RQMoJ4Rm3e4Bz8URTA6PXx9uKXXmA9z4STJjR +1qL5LHJLVeO8TE2KRC9MBClQrZW2E4+pFTVKn6B19TTXmY489C4vZiybunptJPX+CjmZ amvuHZSUoIbcYTDMeipqCtcfTD62Jj1nv1RGjW6w05lb5x+H53d56poZ/yRKiwPKo8g==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796050508; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: MIME-Version:References:In-Reply-To:From:Date:X-Gm-Features: Message-ID:Subject:To:Cc:Content-Type:Content-Transfer-Encoding; bh=XtonR6A2IClIMTzj6FeeA7NI+ZtICXC3ZfmYDFbM2lo=; b=AVgEIGZyUrbflVERdSB6e7caP4Fkna/b2AuMCiJPf7NJN1HLM6v7QP41f9tfEfZZRAKU cUZDBFH4tJ/uzf45/c+UJei22MQyoa7WD/TGn+mrD3ZyjZeI+g6V7rlhxlwooyglEMOCp E3m++ixWptWYg8tPxpfapn4gipnzUsjYtoXVQtXWt5qHlh0I3knelt+RWtWeYXdefdC5z QsUnZepXmuuLbPVF8a+XgsqCejsupNF6NLiv5Xo+mIC7frLCH/CC09GRFXdDeu6FUuZwl WmlFdGMTgHiQ8V5DX60sUdfqhoRhRNuiSjkRLpqU6iAjE194GQ7sTr5ZVpOG7+pEdpw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=mU5OrH8F; arc=pass header.oldest-pass=0 smtp.remote-ip=209.85.214.172
X-Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1A6C606A320 for <git@vger.kernel.org>; Mon, 30 Nov 2026 14:55:07 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C1A6C606A320
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2f0918f9e6cso33693545ad.1 for <git@vger.kernel.org>; Mon, 30 Nov 2026 06:55:07 -0800 (PST)
X-ARC-Seal: i=1; a=rsa-sha256; t=1796050507; cv=none; d=google.com; s=arc-20260327; b=YP2bT9os7+oLLTeywNaQcwn+A/DJUUYQ52E3WpzNalV5MWEnKvIyWOGijXu9tQg1tq tIACDrZQSTKFM/H3rGzMydi8fioh8FuEQH+FIDPaz4FOSDU/sso1dh6Q2vfD1zjKUwMB 62D+pQzeU08D8ivSC6Mj+S++A1V0xikntmI8+LTwYILg+mrLg6NAP8KAnn+msIF6s2U5 Y4Yp+GIdbFSzOTa6D1iBGczWeoRHJdqUgQJaUtSfWbREb4trocNX+8JKA24WfcQCCRAC EuOIpy3J+hPjBYUBUHUDHp1QZywUo3Raii1EAbfpEPnZsBfN1HB230cRXN46u2jKLvKr RY0Q==
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=XtonR6A2IClIMTzj6FeeA7NI+ZtICXC3ZfmYDFbM2lo=; fh=PAw7hej1sGvuTF8p6k7fIhBtRXmUsKdV/f+ygvQOUcQ=; b=jr45/eiBNKIZ4xYrJqfR+6ZDNhrOqyN1fTKkG/lEnRsOmQg1+qQco6VjxPaGG3kopj zWLy4h3MeC3dcfwfX7QIn0LO8kYwL/42mJ6D1rTNt/EQYKLagYL71yDURm9pKLij3NYi y/Cv5Gwjh8flyxg8pan2tiSwd+TxssKvaHrOMdkgsiubveq37z9ajbpiPbUm/0xLyDoS HPbuzZoWf2EwsyyBp5dS2hk4XDNZfSlVAtfobRhbv0F8cj+CvSz9hnEKaPA+9JVXGeOm ZYzS4L33IW2k9ZecKjWh7KdhHLosCQPFAcuSQyX4HRPi3Keuf3MQ0uq0P0lySa47Hvlb X7hA==; darn=vger.kernel.org
X-ARC-Authentication-Results: i=1; mx.google.com; arc=none
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796050507; x=1796655307; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:from:to:cc:subject :date:message-id:reply-to:content-type; bh=XtonR6A2IClIMTzj6FeeA7NI+ZtICXC3ZfmYDFbM2lo=; b=mU5OrH8FPNzMtDJPMz3uavvhBfB207tRcd+CuoNrsy1rsQSXGiSJtJUBxfoHypvIze C8WM0GCD0WOZj4JaZHkTw69h6DcoILrp+Dd4pwg71sOODle9s8H8pJFipxLgKx2nGCQo SX7/uLZmKu+zSB59GwP3h4KzF8YpW7tpzZbefo+g1WovNbk5COw8A11nRW7E18O7DC8K yGU8AfrD5PqGzIToxPOqZAaK326fCjEFlImm/nJ+fmCNnVMFOi8uu1gUuL6py/Vms849 Uzk8w4ye59bLbQIXPh8mAfqppCxlreg37hNidYRt/pDXuiByMgevZyV1Vo+RepEYOrp3 J+kg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796050507; x=1796655307; h=content-transfer-encoding:content-type:cc:to:subject:message-id :date:from:in-reply-to:references:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=XtonR6A2IClIMTzj6FeeA7NI+ZtICXC3ZfmYDFbM2lo=; b=Fc6vqPc08iIoNb2diLMV2VYOf00X7YjGi+m2zhHF1RJEi2OAc5ufpa3SyMZtLsuqQW 8lueX39axvVKqP9swr1tmsPVP4GzJk/7VIZK/b/ddUy2A9ps4m0q+MNW4UYAteD5DZuo k8E14N1NgIUdsp4Fh+elurSbem8Jse6KKFC7srUgCyLCAAs8EIuDfAAwYgve+/aqx6Re x6C9qc6M99QG/LJdh2jB+hId4W30BDxV0I+eSktwF0qD9+/qUR4p/1EB6gb0dgsXuCk7 2URSemr+khZSul6vDjRVN7onLsvLJNrMWpfDtI1jBDuUrVifR0e284g8etQGfwZDZxMW cGiQ==
X-Forwarded-Encrypted: i=1; AL+hErEDG1HJzzQKkROZpYJvUu+HbMeAarEki90qvl3cJMQvw7BWEy+rJwPuhVMbQde1JVI5OBM=@vger.kernel.org
X-Gm-Message-State: APuU4Z+ROUtQ11mOu+tn8Mx/cr0ua1Zb0UbwbzN0lEyHoO19taNEjwJg sORtLzUOPADOLkhtYdV36WKYqoRt3qNyCoZC9TALllQVRmZHwkhik2aAgE0nGvEZ6UOVw3+efNE FZdxdo9F7HDdc852M7TYfDwtLug0h2cPD5GND
X-Gm-Gg: AZFdTZdDMZUjlZ1bXB+wRTUDQpdsybVyXOQFUOuT6pCOBilDHJwZFpqxgiae+8s5wJs 4BudLE2iFaqmT24ahQmT6Gg3n+NV1939X/YbLS7GAo2V3FS2kqSNI3QCib0WcVGvsNHKTQ0OQHz kUu0lPoKUQJOsTAr5lmjXWMMIKMZLxsoRCcB7arYogz7ukFE7tEc82e1QRxzq38Rhv9ckFVXxTF yL7I1X1c7iD3LZVRV+3O86wGeDBauewehjOVaS4XPaYZ6oZbXLteNYTvbp2krqJScdQKHTEe6Qj v8akmcfbDNANlfDBMEa6Jp4+dgrKltHMrIZ3snG0eVFv3SlfG8nLyX/iGhHCi5zChzwm6Mb4h3W yCYAjKrBYY5453dCTMnBjXy42OQQDVpn1mnCrGRU/G1x3JlUdPU2bMySBsm+yzmkeDUfMXg0CCX 4mq9sGDjgECd6+763htHueCJTMxc8XxHBQrdDIMTEr1yxq7NGaXc+AMuOXcp5YQxSYJw==
X-Received: by 2002:a17:902:cec4:b0:2f0:436d:bdc1 with SMTP id d9443c01a7336-2f0436dc431mr314101875ad.33.1796050506959; Mon, 30 Nov 2026 06:55:06 -0800 (PST)
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261123160009.61bf0bce@gandalf.local.home>
X-Gm-Features: Aa3M54punQDP2Le9plWg4XEYoshGKGLDSqmqUfAnx-nDTWOyZTb9S_wOv2v4pME
X-Cc: Mirko Faina <mroik@delayed.space>, git@vger.kernel.org
X-Received-Bytes: 13724