Skip to content

Commit 5d61f4c

Browse files
committed
Add Keep File Locally With git rm as a Git TIL
1 parent 5811268 commit 5d61f4c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1345 TILs and counting..._
13+
_1346 TILs and counting..._
1414

1515
---
1616

@@ -283,6 +283,7 @@ _1345 TILs and counting..._
283283
- [Include Some Stats In Your Git Log](git/include-some-stats-in-your-git-log.md)
284284
- [Intent To Add](git/intent-to-add.md)
285285
- [Interactively Unstage Changes](git/interactively-unstage-changes.md)
286+
- [Keep File Locally With `git rm`](git/keep-file-locally-with-git-rm.md)
286287
- [Last Commit A File Appeared In](git/last-commit-a-file-appeared-in.md)
287288
- [List All Files Changed Between Two Branches](git/list-all-files-changed-between-two-branches.md)
288289
- [List Branches That Contain A Commit](git/list-branches-that-contain-a-commit.md)

git/keep-file-locally-with-git-rm.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Keep File Locally With `git rm`
2+
3+
Let's say I've added a new file `data.json` to my repo as part of the most
4+
recent commit. I realize this isn't the point at which I want to add that file.
5+
So, I do `git rm data.json` and then `git commit --amend` to rework that
6+
commit.
7+
8+
However, when I look in my working tree, or even just my file system, I'll
9+
notice that `data.json` is gone. The `git rm` command completely removed the
10+
file since it was previously an untracked file.
11+
12+
To keep `git rm` from tossing out my file like that, I can include the
13+
`--cached` flag which will remove the file from the index (stages it to be
14+
`deleted`), but restore it to the working directory.
15+
16+
```bash
17+
$ git rm --cached data.json
18+
```
19+
20+
See `man git-rm` for more details on the `--cached` flag.

0 commit comments

Comments
 (0)