【问题标题】:Can I reference a script in 'git diff path/to/file' using it's index instead of it's path?我可以使用它的索引而不是它的路径来引用“git diff path/to/file”中的脚本吗?
【发布时间】:2021-02-02 15:55:36
【问题描述】:

我确实经常想快速检查我所做的更改。使用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 中所做的更改,而不是完整路径。

【问题讨论】:

  • 文件名补全,比如git diff A&lt;tab&gt;_P&lt;tab&gt;S&lt;tab&gt;UI/Tabl&lt;tab&gt;(取决于是否还有其他以A开头的东西等)。输入通常不会那么糟糕。

标签: git git-diff


【解决方案1】:

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

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

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

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

    #!/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 ...

【讨论】:

  • 非常感谢您的帮助!我已经每天多次使用这个脚本,它帮助很大! :)
  • 很高兴知道:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2023-03-20
  • 2018-03-15
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多