Skip to content

Commit b002b71

Browse files
committed
docs: update status-vs-diff-index.md
1 parent 226172d commit b002b71

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

docs/development/advanced/status-vs-diff-index.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Using `status` is friendly for everyday use as developer. The `diff-index` subco
1313

1414
Both `status` and `diff-index` can be used to see a list of paths and how they changed, using `from` and `to` as a pair of paths when moving or renaming a file.
1515

16-
Why use `diff-index` and not `status`? Using the former makes things more **predictable** when parsing output. Since the `from` file is always first, from left to right. While `status` has it on the right, which is hard because it is not always there. There might be other reasons I can't remember, maybe because there is a percentage similarity which shows up in `diff-index` for renames/moves.
16+
Why use `diff-index` and not `status`? Using the former makes things more **predictable** when parsing output. Since the `from` file is always first, from left to right. While `status` has it on the right, which is hard because it is not always there. There might be other reasons I can't remember, maybe because there is a percentage similarity that shows up in `diff-index` for renames/moves.
1717

1818

1919
## status
@@ -35,30 +35,75 @@ $ git status --short
3535
M abc.txt
3636
```
3737

38+
Sample output of multiple lines. Note use of spaces not tabs.
39+
40+
```console
41+
$ git status --short
42+
R LICENSE -> LIC
43+
M docs/development/advanced/status-vs-diff-index.md
44+
M src/test/git/parseOutput.test.ts
45+
```
46+
3847

3948
## diff-index
4049

41-
The less-known `git diff-index` subcommand. This is not so usable for day to to day use in the CLI but is great for scripts, or for a project such as this one.
50+
The lesser-known `git diff-index` subcommand. This is not so usable for day to to day use in the CLI but is great for scripts, or a project such as this one.
4251

4352
```sh
4453
$ git diff-index [FLAGS] PATH
4554
```
4655

4756
> Compare a tree to the working tree or index
4857
49-
- Flags must appear before the path.
58+
Notes:
59+
60+
- Output has tab separator between columns.
5061
- Use `--cached` for _only_ staged changes. Note that the only way to pick up a new file or detect a move/rename pair properly with `diff-index` is to stage changes first and then use this flag.
62+
- Use `--name-status` to show names and status of changed files.
63+
- Use `-M` to detect renames (i.e. move or rename a file, stage both paths, then run the command with this flag to see it appear as `R100` or similar).
5164
- The path is required - `HEAD` works fine.
5265

5366
e.g.
5467

5568
```console
5669
$ git diff-index HEAD
57-
$ :100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M abc.txt
70+
:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M abc.txt
71+
```
72+
73+
```console
5874
$ git diff-index --name-status HEAD
5975
M abc.txt
6076
```
6177

78+
Two files changed, with long paths:
79+
80+
```console
81+
$ git diff-index --name-status HEAD
82+
M docs/development/advanced/status-vs-diff-index.md
83+
M src/test/git/parseOutput.test.ts
84+
```
85+
86+
For a move or rename:
87+
88+
```console
89+
$ mv LICENSE LIC
90+
$ git add .
91+
$ git diff-index --name-status -M HEAD
92+
R100 LICENSE LIC
93+
```
94+
95+
Even though you might be able to select the output in the console as spaces, it is actually tabs. Check with a tool that can show hidden characters.
96+
97+
```console
98+
$ git diff-index --name-status HEAD -M | bat -A
99+
───────┬─────────────────────────────────────────────────────────
100+
│ STDIN
101+
───────┼─────────────────────────────────────────────────────────
102+
1 │ R100├──┤LICENSE├──┤LIC␊
103+
2 │ M├──┤docs/development/advanced/status-vs-diff-index.md␊
104+
3 │ M├──┤src/test/git/parseOutput.test.ts␊
105+
```
106+
62107
### Limitation of diff-index
63108

64109
#### Summary

0 commit comments

Comments
 (0)