首页 > 解决方案 > 我可以使用它的索引而不是它的路径来引用“git diff path/to/file”中的脚本吗?

问题描述

我确实经常想快速检查我所做的更改。使用git status通常如下所示:

$ git status
On branch develop
Your branch is ahead of 'develop' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Assets/_Project/Scripts/Controls/ControllerManager.cs
        modified:   Assets/_Project/Scripts/UI/TabletManager.cs

no changes added to commit (use "git add" and/or "git commit -a")

要检查我所做的更改,我必须使用git diff Assets/_Project/Scripts/UI/TabletManager.cs文件的完整路径,这些路径通常很长。但由于我经常使用这个功能,只更改了一些脚本,我想知道是否有类似快速访问选项的东西?也许通过传递修改文件的索引?喜欢git diff 1查看 TabletManager 中所做的更改而不是完整路径。

标签: gitgit-diff

解决方案


我不认为 git 有你想要的快速访问选项,因为大多数时候自动完成就足够了,正如@torek 在评论中告诉你的那样。

如果你真的想要这个功能,你可以自己创建它。

  1. 首先创建一个专用于 git 扩展的文件夹和cd

  2. 创建一个脚本文件并将其命名为git-easydiff. 现在我将它添加为本地别名,但您甚至可以使用自定义命令扩展 git,这里描述了如何。顺便说一句,一个可能的脚本可能是:

    #!/bin/bash
    changed_files=( $(git diff --name-only) )
    for ((i = 0; i < ${#changed_files[@]}; i++))
    do
        echo "$i - ${changed_files[$i]}"
    done;
    
    read -p "Index of file to git-diff: "
    
    git diff ${changed_files[$REPLY]}
    
  3. 现在别名

    git config alias.easydiff '!/directory_with_git_extensions/git-easydiff'
    

    !开头是必要的,因为该命令不是 git 命令。

您可以将其称为git easydiff,输出为:

0 - file1
1 - file2
Index of file to git-diff: 1
... diff for file2 ...

推荐阅读