Search code examples
gitgit-diff

Can I reference a script in 'git diff path/to/file' using it's index instead of it's path?


I do often want to quickly check on the changes I made. Using git status often looks like this:

$ 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")

To check on the changes I made I would have to use git diff Assets/_Project/Scripts/UI/TabletManager.cs with the full path to the file which are often quile long. But since I use this feature quite often with just a few changed script I wonder whether there is something like a quick access option? Maybe by passing the index of the modified file? Something like git diff 1 to see the changes made in TabletManager instead of the full path.


Solution

  • I do not think git has the quick access option you wish, since most of the times the autocompletion is enough, as @torek told you in the comments.

    If you really want this feature, you can create it yourself.

    1. First create a folder dedicated to git extensions and cd to it

    2. Create a script file and name it something like git-easydiff. Now I am adding it as a local alias but you can even extend git with custom commands, here described how. By the way, a possible script could be:

      #!/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. And now the alias

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

      the ! at the beginning is necessary because the command is not a git command.

    You can call it as git easydiff, the output is:

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