简体   繁体   中英

Search computer for all .git-folder with uncommited changes

I have several project-folders on my machine, all including a .git -directory. I assume that all programmers have been in the situation, where they've forgotten to push something to the server. So is there a way to search all subdirectories for uncommitted/unstaged changes and list them?

Here is an example of my situation:

MainFolder
     |
     |-Project1
     |     |-.git # containing uncommitted changes
     |     |-index.php
     |     |-page_one.php
     |
     |-Project2
     |     |-.git # This git-status is clean
     |     |-index.php
     |     |-another_page.php
     |
     |-Project3
     |     |-.git # containing unstaged changes
     |     |-index.php
     |     |-some_page.php
     |
     |-Project4
           |-.git # containing uncommited changes
           |-index.php
           |-some_page.php

I'd like a list, say:

You need to check these .git-files:
 - MainFolder/Project1/.git 
 - MainFolder/Project3/.git 
 - MainFolder/Project4/.git 

Does find do what you want?

find MainFolder -name .git -print -execdir git status \;
  • -name <pattern> the pattern you are searching for
  • -print print out all hits
  • -execdir <command> in the subdirectory where you got a hit execute command; the trailing semicolon is required, and needs to be escaped, to build the command in the find loop

More detailed information can be found in the man page .

I have a little bash script that I use to do this. It won't get you the tree you're after, but it will tell you the git repo and whether there are any uncommitted changes.

function runCmd
{
  echo `basename $dir` "repository:"
  eval $@
  echo
}

for dir in `ls -d */`
do
  pushd $dir >> /dev/null
  if [ -d ".git" ]
  then
    runCmd $@
  fi
  popd >> /dev/null
done

Save this in a file called allRepo. Then you can run whatever command you want from the top level of you file tree:

allRepo git fetch
allRepo git status

This script is nice because it's not limited to only git commands.

Do you use locate (or its variants mlocate or slocate ) and have locate.db regularly updated? I use the following script:

locate -b \*.git | sed 's!/\.git$!!'| while read d; do
    test -d "$d"/.git || continue
    cd "$d" && test -n "`git status --short`" && echo "$d"
done

PS. locate can return both bare ( xxx.git ) and non-bare ( xxx/.git ) directories, so I strip /.git and test only non-bare repos.

find /Mainfolder -name ".git" -type d | awk -F\/ '{ printf "cd ";for ( i=2;i<=NF-1;i++ ) { printf "%s/",$i } printf "; git status | grep -q \"new file\" && echo \"$(pwd) Needs checking\""}' | sh

Utilising find with awk on one line, you find all directory that are controlled by git, then build the directory name using awk. This is then printed and git-status is executed with the result grepped for "new file". This constructed command is then piped through sh to execute it.

The text "new file" will need to be changed accordingly.

Output:

/Mainfolder/Project1 Needs checking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM