threads / discuss / 53837

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

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

## tl;dr

6 messages between Jul 10, 2020 and Jul 10, 2020.

replies: 5people: 3as markdown or json

Tom Browder· Jul 10, 2020, 12:25 UTC · lore

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· Jul 10, 2020, 12:33 UTC · re: Tom Browder · lore

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

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· Jul 10, 2020, 13:14 UTC · re: Andreas Schwab · lore

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

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· Jul 10, 2020, 14:04 UTC · re: Tom Browder · lore

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

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· Jul 10, 2020, 16:31 UTC · re: Andreas Schwab · lore

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

On Fri, Jul 10, 2020 at 9:04 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
Show 6 quoted lines
> 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· Jul 10, 2020, 22:43 UTC · re: Andreas Schwab · lore

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

At 16:04 +0200 10 Jul 2020, Andreas Schwab <schwab@linux-m68k.org> wrote:
Show 7 quoted lines
>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)

← back to recent threads