首页 > 解决方案 > git 中文件的最后一次提交显示添加的行,但文件不在项目文件夹中

问题描述

使用该命令git log --numstat,我可以看到文件MyFile的最后一次提交显示添加了一些行。这意味着它对MyFile所做的最后一件事是有人在其中添加了一些行。

同时我在项目文件夹中找不到MyFile 。因此,我假设MyFile在某些分支中被引用,但在当前分支中没有。

这是一个正确的假设吗?如果是这样,有没有办法找到哪些分支实际上引用了MyFile

标签: gitgit-log

解决方案


如果发生以下情况,这可以解释:

  1. 该文件已在分支 A 上删除。
  2. 删除发生(按时间) ,在分支 B 上修改了相同的文件。
  3. 分支B合并到分支A,导致冲突,选择删除。

这是一个示例 bash 脚本来演示这一点:

#!/bin/bash -v

git init

echo asdf > asdf.txt && git add . && git commit -m "Add file asdf.txt"

git branch test-branch

rm asdf.txt && git add . && git commit -m "Delete asdf.txt"

git switch test-branch

sleep 1 # pause for one second to make sure commit datetimes are different

echo qwer >> asdf.txt && git add . && git commit -m "Add qwer to asdf.txt"

git switch main

git merge test-branch --strategy=ours --no-edit # cheat to prevent a conflict and skip the other commit

ls -la # prove there are no files

git log --numstat # the most recent commit of asdf.txt is adding a line

推荐阅读