git diff - only show which directories changed

后端 未结 3 1401
礼貌的吻别
礼貌的吻别 2020-12-05 19:51

Is there a way to list only the directories that were changed?

If I\'m at the git root say, ~/project

Files I changed are

~/proje

相关标签:
3条回答
  • 2020-12-05 20:27

    Use git diff with parameter --dirstat, e.g.

    git diff --dirstat HEAD~100..HEAD
    
    0 讨论(0)
  • 2020-12-05 20:35

    this solution works also for files in the root directory:

    git diff --name-only HEAD~1 | awk -F "/*[^/]*/*$" '{ print ($1 == "" ? "." : $1); }' | sort | uniq
    

    Changes in root directory will be listed as .

    The limitation of git diff --dirstat=files,0 HEAD~1 is that it doesn't show changes in the root directory.

    0 讨论(0)
  • 2020-12-05 20:37

    You could use git-diff with the --dirstat parameter.

    In your scenario, let's say you have the following commit:

    $ git diff --name-status HEAD~1
    M       subtool/file1
    M       subtool/file2
    M       subtool3/file1
    

    It would produce the following output:

    $ git diff --dirstat=files,0 HEAD~1
      66.6% subtool/
      33.3% subtool3/
    

    Make sure to add ,0, otherwise git diff will by default only show directories with at least 3% changes. I also chose files as this is the computationally cheapest option and you do not seem to care about specific changes anyway.

    If you are able to use sed you can get rid of the percentage values (you may want to tweak the regular expression a bit to fit your needs):

    $ git diff --dirstat=files,0 HEAD~1 | sed 's/^[ 0-9.]\+% //g'
    subtool/
    subtool3/
    
    0 讨论(0)
提交回复
热议问题