我使用过几次的一个技巧是围绕git
. 假设您使用的是 Bash(其他 shell 也类似),请将以下内容添加到您的~/.bashrc
:
git () {
if [[ $1 == status ]]
then
# User has run git status.
#
# Run git status for this folder. The "command" part means we actually
# call git, not this function again.
command git status .
# And now do the same for every subfolder that contains a .git
# directory.
#
# Use find for the loop so we don't need to worry about people doing
# silly things like putting spaces in directory names (which we would
# need to worry about with things like `for i in $(ls)`). This also
# makes it easier to recurse into all subdirectories, not just the
# immediate ones.
#
# Note also that find doesn't run inside this shell environment, so we
# don't need to worry about prepending "command".
find * -type d -name .git -execdir git status . \;
else
# Not git status. Just run the command as provided.
command git "$@"
fi
}
现在,当您运行时git status
,它实际上会git status
针对当前文件夹和包含其自己文件夹的任何子文件.git
夹运行。
或者,您可以通过按照Chronial 建议的方式编写脚本或将其放入 Git 别名中,将其变成一个新命令。要执行后者,请运行以下命令:
git config --global alias.full-status '!cd ${GIT_PREFIX:-.}; git status .; find * -type d -name .git -execdir git status . \;'
然后你就可以跑来git full-status
做同样的事情了。
(该cd ${GIT_PREFIX:-.}
部分用于将您返回到您运行命令的任何目录;Git 别名默认从存储库的根目录运行。其余的与上面的函数解决方案相同。)