Search code examples
gitsortingfishuniq

How to store an output of "git pull", without deleted files, numbers and duplications, for listing new files in a text file?


I use mostly Fish instead of Bash and Zsh. I want to output a whole fast-forward's git pull since March until today, but only the modified and new files, without number of additions, deletions and modifications, without duplications, to list the new icons for a text file, so I will copy the missed new icons to another branch.

For example:

git pull > new_icons.txt

I want to use these commands, but I need the options to exclude the deleted files, the duplications and the numbers of additions, deletions and modifications

git pull > new_icons.txt 2 > &1
git pull |& tee new_icons.txt
git pull &> new_icons.txt

If you do not know what "number of additions, deletions and modifications" are, for example:

enter image description here

You can see |, numbers, + and -. I want to exclude them when I output in a text file.

And what are the duplications?

For example:

February 7
Fast-forward
   icon.svg | 3+++

February 24
Fast-forward
   icon.svg | 2--

March 7
Fast-forward
   icon.svg | 1++

I want to output only a unique icon.svg.

I am not sure if I can use the command fmt -1 | sort -z | uniq -cd | xargs echo for git pull, like:

git pull |& fmt -1 | sort -z | uniq -cd | xargs echo |& tee new_icons.txt

Solution

  • The output of git pull is just git diff --stat between the commits before/after the pull.

    e.g : run git diff --stat d649d3e f0c56e2, and you should see the same output as in your screenshot.


    Spot the root commit you want (the state of your repo in March), and use git diff.

    For your use case, git diff has other useful options :

    • --name-status will show the names of impacted files, prefixed with A,M or D (resp: Added, Modified or Deleted)
    • --name-only will display the filenames, without further information
    • --diff-filter=[(A|M|D)] will allow you to display information only about files matching the filter

    See the complete docs : git diff
    (I simplified the description of diff-filter and name-status, the full doc can be found here and here)

    The list you are looking for is :

    git diff --no-renames --diff-filter=AM --name-only [basecommit] [branch]
    

    Since you somehow want to "apply a patch" on some other branch, you may also need the list of deleted files (to delete them ...) :

    git diff --no-renames --diff-filter=D --name-only [basecommit] [branch]
    

    The following list is helpful to understand what happened between the two commits :

    git diff --name-status [basecommit] [branch]