有什么工具可以显示我的git仓库的状态以及它们与远程仓库相比前后的差异吗?我想像这样的工具存在:
repo1 +2 <--> remote1
repo1 <--> remote1 +3
repo2 <--> remote +10
第一行显示repo1领先2个提交,第二行显示remote2领先3个提交,第三行仅显示repo2的远程领先10个提交。
.git
文件夹:
find-dirty.sh
#!/bin/bash
# usage: $0 source_dir [source_dir] ...
# where source_dir args are directories containing git repositories
red="\033[00;31m"
green="\033[00;32m"
yellow="\033[00;33m"
blue="\033[00;34m"
purple="\033[00;35m"
cyan="\033[00;36m"
reset="\033[00m"
if [ $# -eq 0 ] ; then
ARGS=( "foldername" "foldername/subfoldername" )
else
ARGS=$@
fi
for i in ${ARGS[@]} ; do
for gitdir in `find $i -name .git` ; do
( working=$(dirname $gitdir)
cd $working
RES=$(git status | grep -E '^# (Changes|Untracked|Your branch)')
STAT=""
grep -e 'Untracked' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT=" $red[Untracked]$reset"
fi
grep -e 'Changes not staged for commit' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $red[Modified]$reset"
fi
grep -e 'Changes to be committed' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $green[Staged]$reset"
fi
grep -e 'Your branch is ahead' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $yellow[Unpushed]$reset"
fi
grep -e 'Your branch is behind' <<<${RES} >/dev/null 2>&1
if [ $? -eq 0 ] ; then
STAT="$STAT $cyan[Unmerged]$reset"
fi
if [ -n "$STAT" ] ; then
echo -e "$working :$STAT"
fi
)
done
done
git fetch --all
,并使用类似这样的脚本(我称之为 `git-repo-status locally):#!/bin/bash
# Taken from http://stackoverflow.com/questions/620650/can-i-easily-update-all-local-git-branches-from-remote-branches-simultaneously/answer-9076361
# tweaked by me to help give status of local versus upstream branches.
main() {
REMOTES="$@";
if [ -z "$REMOTES" ]; then
REMOTES=$(git remote);
fi
REMOTES=$(echo "$REMOTES" | xargs -n1 echo)
echo "$REMOTES" | while read REMOTE; do
git remote show $REMOTE -n \
| awk '/merges with remote/{print $5" "$1}' \
| while read line; do
RB=$(echo "$line"|cut -f1 -d" ");
ARB="refs/remotes/$REMOTE/$RB";
LB=$(echo "$line"|cut -f2 -d" ");
ALB="refs/heads/$LB";
# This is in reference to the local branch.
NBEHIND=$(( $(git rev-list --count $ALB..$ARB 2>/dev/null) +0));
NAHEAD=$(( $(git rev-list --count $ARB..$ALB 2>/dev/null) +0));
if [ "$NBEHIND" -gt 0 -a "$NAHEAD" -gt 0 ]; then
echo "$LB <--> $REMOTE/$RB: -$NBEHIND +$NAHEAD";
elif [ "$NBEHIND" -gt 0 ]; then
echo "$LB <--> $REMOTE/$RB: -$NBEHIND";
elif [ "$NAHEAD" -gt 0 ]; then
echo "$LB <--> $REMOTE/$RB: +$NAHEAD";
fi
done
done
}
main $@
:: git repo-status
master <--> upstream/master: -14
我知道这不是你问题的直接答案,但我认为如果不运行 git fetch --all
并从上游获取版本,你想要的是不可能实现的。如果你愿意这样做,那么上面的内容对你可能有用。
我知道这个问题已经不是那么新鲜了,但我写了一个开源工具来解决这个问题:它可以找到本地的git仓库并追踪它们的状态,在一个地方展示所有本地仓库的当前分支和状态字符串。
这使它成为所有(git)开发任务的完美起点。
如果您正在使用Windows,则可能会享受到额外的效果:
因为使用git时通常没有专用的分支文件夹,例如MyTool\trunk\...
或MyTool\v2.0\...
,所以在Windows文件浏览器中使用时无法看到您正在活跃工作的分支。
因此,这个工具的酷之处就在于,运行时它会自动增强所有文件浏览器窗口,添加打开的仓库的当前分支和状态字符串(如果有的话)。
它是100%开源的,所以请试用并且如果您愿意做出贡献,我会非常高兴。
RepoZ现在带有一个命令行增强工具名为grr,利用了RepoZ聚合的仓库信息,在命令行中显示实时数据加上使用cd
命令跳转到不同的仓库:grr cd RepoZ
快速一行代码,假设您在包含许多git仓库文件夹的文件夹中:
find . -maxdepth 1 -type d -exec sh -c "(cd '{}' && pwd && git status)" \;