如何获取 git 存储库中特定文件的更改历史记录

发布于 2024-09-01 12:29:03 字数 239 浏览 4 评论 0原文

我想做类似的事情:

git history my_file

可能的输出

2010-05-16
+ add this line
+ more code here

2010-05-15
+ delete code below
- bad code
- more bad codd

2010-05-12
+ changes made here

I would like to do something like:

git history my_file

possible output

2010-05-16
+ add this line
+ more code here

2010-05-15
+ delete code below
- bad code
- more bad codd

2010-05-12
+ changes made here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-09-08 12:29:03

尝试:

git log -p -- filename

Try:

git log -p -- filename
温柔少女心 2024-09-08 12:29:03

最接近您想要的内容是来自 git log:

git log -p -U0 --pretty=format:%ai -- filename
  • -p: 正如 Charles Bailey他的答案:生成补丁
  • -U0:使用 0 行上下文(而不是通常的三行)生成差异。
  • --pretty=format:%ai:在补丁输出之前添加作者日期,ISO 8601 格式。

例子:

/c/Prog/Git/git2/git (master)
$ git log -p -U0 --pretty=format:%ai -- wt-status.c
2010-03-24 16:25:43 -0700
2010-03-13 23:00:27 +0100
diff --git a/wt-status.c b/wt-status.c
index e0e915e..5848f1c 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -306,0 +307,2 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)
+       if (!s->show_untracked_files)
+               DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);

2010-03-08 22:58:09 -0800
diff --git a/wt-status.c b/wt-status.c
index 5807fc3..dcaec7f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -292,0 +293 @@ static void wt_status_collect_changes_index(struct wt_status *s)
+       struct setup_revision_opt opt;
@@ -295,2 +296,4 @@ static void wt_status_collect_changes_index(struct wt_status *s)
-       setup_revisions(0, NULL, &rev,
-               s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
+       memset(&opt, 0, sizeof(opt));
+       opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
+       setup_revisions(0, NULL, &rev, &opt);
+

The closest to what you want would be, from git log:

git log -p -U0 --pretty=format:%ai -- filename
  • -p: as Charles Bailey mentions in his answer: Generate patch
  • -U0: Generate diffs with 0 lines of context instead of the usual three.
  • --pretty=format:%ai: precede the patch output with the author date, ISO 8601 format.

Example:

/c/Prog/Git/git2/git (master)
$ git log -p -U0 --pretty=format:%ai -- wt-status.c
2010-03-24 16:25:43 -0700
2010-03-13 23:00:27 +0100
diff --git a/wt-status.c b/wt-status.c
index e0e915e..5848f1c 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -306,0 +307,2 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)
+       if (!s->show_untracked_files)
+               DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);

2010-03-08 22:58:09 -0800
diff --git a/wt-status.c b/wt-status.c
index 5807fc3..dcaec7f 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -292,0 +293 @@ static void wt_status_collect_changes_index(struct wt_status *s)
+       struct setup_revision_opt opt;
@@ -295,2 +296,4 @@ static void wt_status_collect_changes_index(struct wt_status *s)
-       setup_revisions(0, NULL, &rev,
-               s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
+       memset(&opt, 0, sizeof(opt));
+       opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
+       setup_revisions(0, NULL, &rev, &opt);
+
一身骄傲 2024-09-08 12:29:03

如果将文件传递给 gitk(随 Git 安装一起提供),还会提供文件历史记录的漂亮 UI 视图。

在 Windows 上...

gitk.cmd <file>

A nice UI view of the file history is also given if you pass the file to gitk, which comes with your Git installation.

On Windows...

gitk.cmd <file>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文