# "git rm" could be safer and have a better help msg

6 messages from 2020-07-10 to 2020-07-10. Participants: Tom Browder, Andreas Schwab, Aaron Schrab.
Thread: https://gitlist.dev/t/53837

## Tom Browder, 2020-07-10 12:25

Subject: "git rm" could be safer and have a better help msg
Message-ID: <CAFMGiz97fcw_nvaa=Xo-roTtB1o25qO06=PHGSrBzv4k_zGqaw@mail.gmail.com>
URL: https://gitlist.dev/e/CAFMGiz97fcw_nvaa%3DXo-roTtB1o25qO06%3DPHGSrBzv4k_zGqaw%40mail.gmail.com

```
I often need to remove a file from Git but leave it as is, changed or not.
The only safe way to do that as far as I know is:

    $ git rm -f --cached myfile   # many thanks to @jast on IRC #git

The "git help rm" command yields copious information but nowhere can I find
a clear statement about safely removing a modified file from Git without
removing it from the file system.

I am going to create an alias to help me, but I believe the brute force
removal should be a different option, say, '-F' ('--Force'), and the
existing '-f' ('--force') ought to be safe operations as far as the file
system is concerned.

Best regards,

-Tom

```

## Andreas Schwab, 2020-07-10 12:33

Subject: Re: "git rm" could be safer and have a better help msg
Message-ID: <878sfr1rdm.fsf@igel.home>
URL: https://gitlist.dev/e/878sfr1rdm.fsf%40igel.home
In-Reply-To: <CAFMGiz97fcw_nvaa=Xo-roTtB1o25qO06=PHGSrBzv4k_zGqaw@mail.gmail.com>

```
On Jul 10 2020, Tom Browder wrote:

> I often need to remove a file from Git but leave it as is, changed or not.
> The only safe way to do that as far as I know is:
>
>     $ git rm -f --cached myfile   # many thanks to @jast on IRC #git

Why do you need -f?

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

```

## Tom Browder, 2020-07-10 13:14

Subject: Re: "git rm" could be safer and have a better help msg
Message-ID: <CAFMGiz8+pjskPpPqVgQUevKdWgxPvCKBcmx-07E2yizDqhm9mg@mail.gmail.com>
URL: https://gitlist.dev/e/CAFMGiz8%2BpjskPpPqVgQUevKdWgxPvCKBcmx-07E2yizDqhm9mg%40mail.gmail.com
In-Reply-To: <878sfr1rdm.fsf@igel.home>

```
On Fri, Jul 10, 2020 at 7:33 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
> >     $ git rm -f --cached myfile   # many thanks to @jast on IRC #git
> Why do you need -f?

If the existing file has been modified, with 'rm'  the '--cached'
option doesn't remove it from Git
unless one uses the '-f' option with it.


-Tom

```

## Andreas Schwab, 2020-07-10 14:04

Subject: Re: "git rm" could be safer and have a better help msg
Message-ID: <874kqf1n65.fsf@igel.home>
URL: https://gitlist.dev/e/874kqf1n65.fsf%40igel.home
In-Reply-To: <CAFMGiz8+pjskPpPqVgQUevKdWgxPvCKBcmx-07E2yizDqhm9mg@mail.gmail.com>

```
On Jul 10 2020, Tom Browder wrote:

> If the existing file has been modified, with 'rm'  the '--cached'
> option doesn't remove it from Git
> unless one uses the '-f' option with it.

It doesn't?  Worksforme.

$ echo >> zlib.c
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   zlib.c

no changes added to commit (use "git add" and/or "git commit -a")
$ git rm --cached zlib.c
rm 'zlib.c'
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    zlib.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        zlib.c


Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

```

## Tom Browder, 2020-07-10 16:31

Subject: Re: "git rm" could be safer and have a better help msg
Message-ID: <CAFMGiz_5Jop2nVXJuMOR7NT9gKEygueic1m5-yw8BrkDgZ-O3w@mail.gmail.com>
URL: https://gitlist.dev/e/CAFMGiz_5Jop2nVXJuMOR7NT9gKEygueic1m5-yw8BrkDgZ-O3w%40mail.gmail.com
In-Reply-To: <874kqf1n65.fsf@igel.home>

```
On Fri, Jul 10, 2020 at 9:04 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
> On Jul 10 2020, Tom Browder wrote:
> > If the existing file has been modified, with 'rm'  the '--cached'
> > option doesn't remove it from Git
> > unless one uses the '-f' option with it.
>
> It doesn't?  Worksforme.

Thanks, Andreas, that does indeed work. I must have done something
wrong, so I'll pay closer attention to what I'm doing.

Best regards,

-Tom

```

## Aaron Schrab, 2020-07-10 22:43

Subject: Re: "git rm" could be safer and have a better help msg
Message-ID: <20200710224335.GB1346@pug.qqx.org>
URL: https://gitlist.dev/e/20200710224335.GB1346%40pug.qqx.org
In-Reply-To: <874kqf1n65.fsf@igel.home>

```
At 16:04 +0200 10 Jul 2020, Andreas Schwab <schwab@linux-m68k.org> wrote:
>On Jul 10 2020, Tom Browder wrote:
>
>> If the existing file has been modified, with 'rm'  the '--cached'
>> option doesn't remove it from Git
>> unless one uses the '-f' option with it.
>
>It doesn't?  Worksforme.

It needs to be forced iff the cached version doesn't match either what's 
in HEAD or on the file system. So when there's potential to lose work 
similar to other situations which require specifying a force option.

     $ git rm --cached a
     error: the following file has staged content different from both the
     file and the HEAD:
         a
     (use -f to force removal)

```
