From 497b0ff3b75f86ce9ef37940caf785f1b22a6627 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 4 Mar 2025 18:05:58 -0600 Subject: [PATCH] Add Files With Local Changes Cannot Be Removed as a Git TIL --- README.md | 3 ++- ...es-with-local-changes-cannot-be-removed.md | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 git/files-with-local-changes-cannot-be-removed.md diff --git a/README.md b/README.md index 400c48c..305c478 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1607 TILs and counting..._ +_1608 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -327,6 +327,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Exclude A File From A Diff Output](git/exclude-a-file-from-a-diff-output.md) - [Excluding Files Locally](git/excluding-files-locally.md) - [Extend Git With Custom Commands](git/extend-git-with-custom-commands.md) +- [Files With Local Changes Cannot Be Removed](git/files-with-local-changes-cannot-be-removed.md) - [Find And Remove Files That Match A Name](git/find-and-remove-files-that-match-a-name.md) - [Find The Date That A File Was Added To The Repo](git/find-the-date-that-a-file-was-added-to-the-repo.md) - [Find The Initial Commit](git/find-the-initial-commit.md) diff --git a/git/files-with-local-changes-cannot-be-removed.md b/git/files-with-local-changes-cannot-be-removed.md new file mode 100644 index 0000000..0b0d0b6 --- /dev/null +++ b/git/files-with-local-changes-cannot-be-removed.md @@ -0,0 +1,26 @@ +# Files With Local Changes Cannot Be Removed + +This is a nice quality-of-life feature in `git` that should help you avoid +accidentally discarding changes that won't be retrievable. + +```bash +❯ git rm .tool-versions +error: the following file has local modifications: + .tool-versions +(use --cached to keep the file, or -f to force removal) +``` + +My `.tool-versions` file has some local changes. I don't realize that and I go +to issue a `git rm` command on that file. Instead of quietly wiping out my +changes, `git` lets me know I'm doing something destructive (these local +changes won't be in the diff or the reflog). + +I can force the removal if I know what I'm doing with the `-f` flag. Or I can +take the two step approach of calling `git restore` on that file and then `git +rm`. + +The `--cached` flag is also interesting because it doesn't actually delete the +file from my file system, but it does stage the file deletion with `git`. That +means the file now shows up as one of my untracked files. + +See `man git-rm` for more details.