Article View: linux.kernel.git
Article #4150Re: Prevent git reset --hard from losing data
From: Steven Rostedt
Date: Mon, 30 Nov 2026 17:11
Date: Mon, 30 Nov 2026 17:11
113 lines
13564 bytes
13564 bytes
On Mon, 30 Nov 2026 09:54:55 -0500
"D. Ben Knoble" <ben.knoble@gmail.com> wrote:
> Hi Steven, forgive the incoming question if it appears rude (I am
> genuinely curious):
I take no offense to this ;-)
>
> 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]
Which is pretty much always the case when working on the Linux kernel. I
have branches based on various rc releases and each one has thousands of
changes between them.
>
> Here a simple "git restore ." would restore the working tree to match
> the index (which should be equivalent to HEAD), right?
Hmm, I actually wasn't familiar with the restore option. Thanks!
>
> 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.)
>
My workflow usually contains me working on several different topic branches
at the same time. (I'll switch between them quickly, while one is being
tested so I can work on another as the tests can last for 10s of minutes).
These branches have a development, for-next, and urgent focus. The
development is me trying all sorts of things. When I feel ready, I'll
usually cherry pick to the for-next branch.
When I checkout a for-next and cherry pick from the development, I
sometimes realize I screwed something up and need to reset the branch back
to its original state. If the work I was doing was a 'work-in-progress' I
tend to create a branch to keep that work before doing the reset. Hence,
something like this:
$ git checkout trace/trace/for-next
$ git cherry-pick trace/trace/devel SHA-1..SHA-2
[ work on it, rebase, etc ]
Realize it isn't what I want:
$ git branch trace/save-this-work
$ git reset --hard <original-sha>
What I sometimes forget, is that I didn't commit my last updates, so the
branch with save-this-work doesn't include them. After the reset --hard, I
lost what I had done.
Over the years I just default to "reset --hard" all the time. This may be
my own issue and I can accept that. In which case, I'll just create a
"git-reset" script I use that adds the "--hard" option but does a git diff
first and asks me if I want to continue if it finds any changes.
-- Steve
Technical Details
Message-ID:
<20261130121137.2fbafcef@gandalf.local.home>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76713-linux-kernel-git=dewy.ruReferences:
<20261120145444.13061eb7@gandalf.local.home> <awC0MJDYS88yEtgm@exploit> <20261123160009.61bf0bce@gandalf.local.home> <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76713-linux-kernel-git=dewy.ru From: rostedt@goodmis.org (Steven Rostedt) Newsgroups: linux.kernel.git Subject: Re: Prevent git reset --hard from losing data Date: Mon, 30 Nov 2026 12:11:37 -0500 Organization: Usenet Network Message-ID: <20261130121137.2fbafcef@gandalf.local.home> References: <20261120145444.13061eb7@gandalf.local.home> <awC0MJDYS88yEtgm@exploit> <20261123160009.61bf0bce@gandalf.local.home> <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: usenet.network; logging-data="1479984"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:Zb2f9NjM+zGGSH6ZJrNUDhBXMHQ= sha256:zbNa1P6SwP/Wrn8+88wV5vIh6CD+oRpnao0oG6RWXDc= sha1:SIO6ZRwcHSaJfBaGd/bUsRqVCHA= sha256:atgoMYqbBdPCXixZnY/7ptgaGph21O0gHHt2zaIJ7rQ= 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 42450125FF8; Mon, 30 Nov 2026 20:12:00 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.05 X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, 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, UNPARSEABLE_RELAY=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key) header.d=goodmis.org 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 qK1cykHu76OJ; Mon, 30 Nov 2026 20:11:56 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-76713-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=goodmis.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id B5BE2125FF7; Mon, 30 Nov 2026 20:11:45 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id D8D202EE4B; Mon, 30 Nov 2026 17:11:43 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail; t=1796058703; b=QLW6Rx6Rb1P+/RYhlSUobNwj1OR7INWfC3CpcY0mJzeH8iTf7ylseTnos/oqPG0Kfi1n uNWE8lCyFCh+Zik7OCuZ1tjh1e2lSRjxl48criuhARSiKPGu/lVCwSxy+nP5Kkx3/zTzO JYaW4aPw/GA5epTZ5dVrsCzaTqsSxIxQPMFFLxJMIcubD4FjU3UxXYd6iLyouxzazbXV8 rmK/Vbv96azRigH9oIWGrk6wJGciN4GeDTi2r+9Jtnopb2r52FwGEFGnpVpDUOjwbN1Bj D88TNA5M320dH7Kiv7LPjbLIg6c4IukpF64+w8zzyADcHBobLoiG1lVTnM6FFXd9hUg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796058703; h=X-Original-To:Received:DMARC-Filter:Received:Received:Date:From:To: Cc:Subject:Message-ID:In-Reply-To:References:X-Mailer:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Type: Content-Transfer-Encoding:X-Rspamd-Queue-Id:X-Stat-Signature: X-Rspamd-Server:X-Session-Marker:X-Session-ID:DKIM-Signature:X-HE-Tag: X-HE-Meta; bh=LCQavPmJroLB6aPpMl+tWdHaBeaH2lymfLoAYDTvHuE=; b=NXQel87xCD4LlPFarzXFdAEXDfFQbNe8v/YKw2/mIrSwsSOY+uWQ3tIqrShMAxLyTT3P V1UiA1JK7AvUs/jkD8WOj0A6Rbdw7i35WP3eJGUFUAKspr/n/WkySEhni4mEh1bMxim5V DdrNk1Ue+SkyaoOCsXaCd4xzgSiOCtD+McpoCtbDsX8GnGO5GseojrI+ghQoB0ZxMoSEz C+er1tjKr5tt1YsFSWE8Ic+wn5ABHNLWP+3VeoVLYyAmZ4Pm0UiOHL9f5poTxPq7lIPal UUQUBxcuQ/sj9oDuBK8i84TtkR4rL7jVlGr2s0YsAKwj3sN+6bEx57nfxTpeiDsojWw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=zo8Am+8V; dmarc=pass header.from=goodmis.org; spf=pass smtp.mailfrom=goodmis.org; arc=fail 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 AF81E606A32C; Mon, 30 Nov 2026 17:11:43 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=zo8Am+8V X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=216.40.44.17 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796058702; b=k6rVDbiG4fp0Gy/BC+02JptQ2vNby3vf2wg0Gm6S+vsuvVBZZ2v5ZEkx59V7gaW65M/e Te1yZayHOZIF5oUzlqrlM2oqIcqxXMQ3Ep6o2KYrUJxboLkh7/N4U2nY0+hD/Ikro8hvf MrfqnUriE9f0Iaqibma48VaCL3zZfXWjgkfl2fNuBkaOOKrGCz71tZoxhoV2wdkYSaP8r +l5punKK3aXfmTY32AGdB8SJbXjSGHuX4J3HVebBbW2h9g9JaHEDYZ4yDXfI7zo02zqiN Br4P7Z09lNxoITdIAxjGV9E1tW39Q0d/cu9ZIE8YqgJPRisNVoHXY6Tg7wMRVQYJi1g== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796058702; h=DMARC-Filter:Received:Received:Date:From:To:Cc:Subject:Message-ID: In-Reply-To:References:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding:X-Rspamd-Queue-Id:X-Stat-Signature: X-Spam-Status:X-Rspamd-Server:X-Session-Marker:X-Session-ID: DKIM-Signature:X-HE-Tag:X-HE-Meta; bh=LCQavPmJroLB6aPpMl+tWdHaBeaH2lymfLoAYDTvHuE=; b=THcLEI4blwOqLE48Z2yXZtNUn/DQKtf1QXGQfzeJIkdIYYdhL8tC/P+Q/Pys+a2PLTMj KO0FplfaAVeGvK31iRMeGlKFJYbseJzlwvmrrux0tzHQVWGtZo2sZtkz8xMnB6RJqokmy xdZOnxm3NRMwXJ4tOFJNytQWI2I3ARnD/CVBmX1ArITI3ofKwpZ1QbDdwuWQh3XdeCWT+ igzR86WvOpDZkynvC8Ku8t8lvDKI09gaFZQOcEyRHEy+FbiQJoH1ouO4XypPUfvdlQteZ 7i+nxEw1TxSp8TtNxzyhLnNIiesj+ajRs5jrn0QA7bNxxkjaQ5cjy1whVos/Zx+9DKQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=goodmis.org; spf=pass smtp.mailfrom=goodmis.org; dkim=pass header.d=goodmis.org header.i=@goodmis.org header.a=rsa-sha256 header.s=dkim1 header.b=zo8Am+8V; arc=none smtp.remote-ip=216.40.44.17 X-Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13894606A31C for <git@vger.kernel.org>; Mon, 30 Nov 2026 17:11:41 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 13894606A31C X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=goodmis.org X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=goodmis.org X-Received: from omf19.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8FEFCA84B9; Mon, 30 Nov 2026 17:11:40 +0000 (UTC) X-Received: from [HIDDEN] (Authenticated sender: rostedt@goodmis.org) by omf19.hostedemail.com (Postfix) with ESMTPA id 099EC20027; Mon, 30 Nov 2026 17:11:38 +0000 (UTC) X-Cc: Mirko Faina <mroik@delayed.space>, git@vger.kernel.org X-In-Reply-To: <CALnO6CBXGn4SneyZvSL6PztGBkFJ9r-PQnv3CkVL9CYvm4F0aQ@mail.gmail.com> X-Mailer: Claws Mail 3.20.0git84 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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-Rspamd-Queue-Id: 099EC20027 X-Stat-Signature: 9p5brp3eeerbq8pu8wxasbe83ujysj8b X-Rspamd-Server: rspamout03 X-Session-Marker: 726F737465647440676F6F646D69732E6F7267 X-Session-ID: U2FsdGVkX1/a3rMz8+yPMJjsmKm/3Ml2ZvzEbqF4wbs= X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=goodmis.org; h=date:from:to:cc:subject:message-id:in-reply-to:references:mime-version:content-type:content-transfer-encoding; s=dkim1; bh=LCQavPmJroLB6aPpMl+tWdHaBeaH2lymfLoAYDTvHuE=; b=zo8Am+8VMCdXVCK5URQDPKORTb6LqiLPcEQPxLSztXklxzo8+tFf6AoyfnrX4xJpB8qrs4TaBzdTHjRA/IY+RjhA5tV8UEOOpXBSmec5cp5b75FPoqkkcWKFnZPBX0N4LrL7+Z+1rx9ukh07GCpO8GT1bUjZomeXBmTBTvCeoQY= X-HE-Tag: 1796058698-463940 X-HE-Meta: U2FsdGVkX1+xzqRujyYAZaup+HmwQ6d1HCiDnpNLMGmIebRdDpRc+02T2wkR/lJkpybl51Fd/DcToCEYcPdYXnAUjvpNZBw4RolLNTAk9QowW8zigZdSwAt8Rqha32IZV03LqmXTOCnzkCfYJitOFpFpUNjJAFrkqYAr5H8HuQY6jCoAgcQ7pRD+r4xNZmr6b8Tr6l4F0Pzo8kuu4x48yl9sAJ99zLFkNFWQ72yaCdf+i4UzqQxdFFQCdHS/JGnw+kNmhkNE6rVtSv9dsuMHv/NFt07BfTxiwg2E/rKl32NPsPnb6nW1nZTK/745PnGLCQD19Rva5nFDAZRMp0NKC6CMqHGijqkn4DvjJdKqdJK0R9kQJtqPXEMc6fvyo6K4xXLKRYgx7PYyN1GXSm/0Mg==