From 5d61f4c9d7f019e07d31c4aaeb5a0b18d8b2e5f2 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 27 Oct 2023 12:04:48 -0500 Subject: [PATCH] Add Keep File Locally With git rm as a Git TIL --- README.md | 3 ++- git/keep-file-locally-with-git-rm.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 git/keep-file-locally-with-git-rm.md diff --git a/README.md b/README.md index ae67521..6d86891 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). -_1345 TILs and counting..._ +_1346 TILs and counting..._ --- @@ -283,6 +283,7 @@ _1345 TILs and counting..._ - [Include Some Stats In Your Git Log](git/include-some-stats-in-your-git-log.md) - [Intent To Add](git/intent-to-add.md) - [Interactively Unstage Changes](git/interactively-unstage-changes.md) +- [Keep File Locally With `git rm`](git/keep-file-locally-with-git-rm.md) - [Last Commit A File Appeared In](git/last-commit-a-file-appeared-in.md) - [List All Files Changed Between Two Branches](git/list-all-files-changed-between-two-branches.md) - [List Branches That Contain A Commit](git/list-branches-that-contain-a-commit.md) diff --git a/git/keep-file-locally-with-git-rm.md b/git/keep-file-locally-with-git-rm.md new file mode 100644 index 0000000..62e8480 --- /dev/null +++ b/git/keep-file-locally-with-git-rm.md @@ -0,0 +1,20 @@ +# Keep File Locally With `git rm` + +Let's say I've added a new file `data.json` to my repo as part of the most +recent commit. I realize this isn't the point at which I want to add that file. +So, I do `git rm data.json` and then `git commit --amend` to rework that +commit. + +However, when I look in my working tree, or even just my file system, I'll +notice that `data.json` is gone. The `git rm` command completely removed the +file since it was previously an untracked file. + +To keep `git rm` from tossing out my file like that, I can include the +`--cached` flag which will remove the file from the index (stages it to be +`deleted`), but restore it to the working directory. + +```bash +$ git rm --cached data.json +``` + +See `man git-rm` for more details on the `--cached` flag.