Skip to content

Commit eff6a9c

Browse files
committed
git log follow
1 parent 2a387f8 commit eff6a9c

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,110 @@ git log --pretty=oneline
362362

363363
![alt tag](https://i.imgur.com/jz2cwUA.jpg)
364364

365-
如果你想要只看某個檔案或某個資料夾的變動, 可以這樣使用, 範例如下
365+
如果你想要看某個檔案或某個資料夾的變動, 可以這樣使用, 範例如下
366+
367+
* [Youtube Tutorial - Git Log:資料夾移動後 Log 消失?用 --follow 找回檔案歷史!(等待新增)](xx)
366368

367369
```cmd
368370
git log -- folder
369371
```
370372

373+
如果追蹤個別檔案, 可以加上 `--follow`
374+
375+
```cmd
376+
git log --follow -- folder/demo.py
377+
```
378+
379+
舉個例子, 我有一個資料夾結構如下
380+
381+
```cmd
382+
❯ tree
383+
.
384+
├── production
385+
│   ├── a
386+
│   │   ├── a1.txt
387+
│   │   ├── a2.txt
388+
│   │   └── a3.txt
389+
│   └── b
390+
│   └── b1.txt
391+
└── test
392+
```
393+
394+
`production` 下的歷史紀錄
395+
396+
```cmd
397+
❯ git log -- production/a
398+
399+
* b9a481b - a3.txt
400+
* 02fce19 - a2.txt
401+
* 1e4aa1b - create
402+
```
403+
404+
假設今天把 `production` 底下的 `a` 資料夾移動到 `test` 資料夾底下,
405+
406+
資料夾結構變成如下, 然後 commit
407+
408+
```cmd
409+
❯ tree
410+
.
411+
├── production
412+
│   └── b
413+
│   └── b1.txt
414+
└── test
415+
└── a
416+
├── a1.txt
417+
├── a2.txt
418+
└── a3.txt
419+
```
420+
421+
目前整個 repo 的 git log 如下
422+
423+
```cmd
424+
❯ git log
425+
426+
* 5707ce2 - move
427+
* b9a481b - a3.txt
428+
* 02fce19 - a2.txt
429+
* 1e4aa1b - create
430+
* 1b7fb87 - Initial commit
431+
```
432+
433+
然後再看 `a` 資料夾, 如下
434+
435+
```cmd
436+
❯ git log -- test/a
437+
438+
* 5707ce2 - move
439+
```
440+
441+
你會發現前面的 `a2.txt` 以及 `a3.txt` 的 git 的追蹤紀錄消失了 !!!
442+
443+
方法一, 追蹤原資料夾,
444+
445+
雖然這個檔案已經不存在了, 但是對 git 來說紀錄還是在的
446+
447+
```cmd
448+
# 這邊你會發現資料夾已經不存在的, 因為已經移動了
449+
❯ ls production/a
450+
451+
ls: cannot access 'production/a': No such file or directory
452+
453+
# 但是還是可以追蹤到歷史紀錄
454+
❯ git log -- production/a
455+
456+
* 5707ce2 - move
457+
* b9a481b - a3.txt
458+
* 02fce19 - a2.txt
459+
* 1e4aa1b - create
460+
```
461+
462+
方法二, 加上 `--follow`, 這個可以追蹤個別檔案 (移動後的資料夾不要用它, 因為會看不到過去紀錄)
463+
371464
```cmd
372-
git log -- folder/demo.py
465+
❯ git log --follow -- test/a/a1.txt
466+
467+
* 5707ce2 - move
468+
* 1e4aa1b - create
373469
```
374470

375471
另外底下也是一個看 log 的方式( 很酷 :satisfied:),有 GUI 的感覺( 來源為文章最後的連結 )

0 commit comments

Comments
 (0)