1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Keep File Locally With git rm as a Git TIL

This commit is contained in:
jbranchaud
2023-10-27 12:04:48 -05:00
parent 5811268d3f
commit 5d61f4c9d7
2 changed files with 22 additions and 1 deletions

View File

@@ -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.