git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Anselm Schüler" <mail@anselmschueler.com>
Cc: git@vger.kernel.org
Subject: Re: Why does git-status suggest different commands to unstage files depending on whether there is a commit yet or not?
Date: Tue, 16 Sep 2025 13:48:15 -0700	[thread overview]
Message-ID: <xmqqldmem8ds.fsf@gitster.g> (raw)
In-Reply-To: <de49d686-73fa-4bb9-9ec6-b65d5432a30a@anselmschueler.com> ("Anselm Schüler"'s message of "Tue, 16 Sep 2025 22:34:39 +0200")

Anselm Schüler <mail@anselmschueler.com> writes:

> $ git status
> [...]
> No commits yet
>
> Changes to be committed:
>    (use "git rm --cached <file>..." to unstage)
> [...]

As this is a very initial commit, any file you are including would
only exist in the index and in the working tree files.  The index is
where you prepare the contents of the commit you are going to
create, and "git rm --cached <file>" is the way to remove <file>
from there without losing or clobbering the <file> in the working
tree.  As you do not have a commit yet, you wouldn't have anywhere
to "restore" from, would you?

> After a commit has been made, git-status suggests using git-restore instead:
>
> $ git status
> [...]
> Changes to be committed:
>    (use "git restore --staged <file>..." to unstage)
> [...]

Compared to the previous situation, you do have a commit, so you can
restore to the version in that commit.  During the course of
development that led you to this state, you may have added <file> in
a commit way before the current commit, and you may have made
changes to the <file> multiple times in different commits before the
current commit.  "git rm --cached <file>" would not be how you would
go back to the version in the current commit in such a situation, as
it would take you to the state _before_ you originally added that
file.  You would "restore" the contents in the index to that of the
current commit (i.e. HEAD) to go back to the state.

So, isn't a short answer to the "why" question, "because that is
what you need to do", I guess?

  reply	other threads:[~2025-09-16 20:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-16 20:29 Why does git-status suggest different commands to unstage files depending on whether there is a commit yet or not? Anselm Schüler
2025-09-16 20:34 ` Anselm Schüler
2025-09-16 20:48   ` Junio C Hamano [this message]
2025-09-16 20:52 ` D. Ben Knoble
2025-09-17 12:12   ` Junio C Hamano
2025-09-17 15:33     ` D. Ben Knoble
     [not found] <8e1905c7-7744-444f-9a39-ca809edb6896@anselmschueler.com>
2025-09-16 21:51 ` Fwd: " Anselm Schüler
2025-09-17  8:03   ` Chris Torek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqldmem8ds.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=mail@anselmschueler.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).