如何知道本地回购与远程回购不同,无需获取? [英] How to know local repo is different from remote repo, without fetch?
问题描述
您可以使用 git ls-remote
plumbing命令获取遥控器无状态。
在这里,我们使用git本身作为轻量级数据库,以跟踪远程的状态。
b将以下内容放入脚本中;您可以稍后启用它作为git别名shell函数以方便使用。在你的仓库中运行。
REMOTE_SUM = $(git ls-remote --tags - heads 2> / dev / null | git hash-object --stdin)
如果git cat-file -e $ REMOTE_SUM
然后
echo远程检查总结为最新。
else
echo远程更改,获取...
git ls-remote --tags --heads 2> / dev / null | \
git hash-object -w --stdin&> / dev / null
git fetch
fi
为了清楚起见,省略了一些必要的错误检查,并且重复了代码。
解释
使用 git ls-remote --tags - heads
的提示会产生如下输出:
从/ home / user / tmp / repo2
777201715768a4d82f374f7224e68164a916ac1f refs / heads / bar
78981922613b2afb6025042ff6bd878ac1994e85 refs / heads / master
...
反过来,我们通过 git hash-object --stdin
将上述远程回购的图片散列为单个散列,并检查我们是否以前通过在git中用 git cat-file -e
查询hash。如果我们没有看到它,那么远程图片必须改变,我们首先在git中用 git hash-object -w
来记录它,以适应拉和提交之间的比赛在遥控器上,然后继续取回遥控器。
可以将它与git预取功能集成在一起:预取钩子功能在git中,但这不在这个答案的范围之内。
附录
请注意,以上将在git中生成松散对象,偶尔需要使用 git gc
和可能的来垃圾收集。 -prune
明确。
此外,只要提交不会故意重新排列,分支提示仍然是相同。这会很/不常见/并且违背git改变推送状态的指导原则,但是,最糟糕的情况是你跳过了抓取。
另外请注意, ls-remote
可以在单个远程机器上运行。要使用多个遥控器,您必须通过使用 git remote show
生成遥控器列表来扩展脚本,并依次处理每个遥控器。
I got tens of repos, my script should update them if any difference happened, new commits, new tag, new branch. Fetch is kind of slow for tens of repos in my case, I'd like to know if there is any quick command could meet my requirement.
You can use the git ls-remote
plumbing command to obtain the state of the remotes without fetch.
Here, let’s use git itself as a lightweight database, to keep track of the state of the remote.
Put the following in a script; you can enable it later as a git alias shell function for convenience. Run inside your repo.
REMOTE_SUM=$(git ls-remote --tags --heads 2>/dev/null | git hash-object --stdin)
if git cat-file -e $REMOTE_SUM
then
echo Remote check-summed up-to-date.
else
echo Remote changed, fetching...
git ls-remote --tags --heads 2>/dev/null | \
git hash-object -w --stdin &>/dev/null
git fetch
fi
Some of the necessary error checking was omitted, and code was duplicated for sake of clarity.
Explanation
Listing all the remote tips with git ls-remote --tags --heads
generates output such as:
From /home/user/tmp/repo2 777201715768a4d82f374f7224e68164a916ac1f refs/heads/bar 78981922613b2afb6025042ff6bd878ac1994e85 refs/heads/master ...
In turn we hash the above picture of the remote repo as a single hash via git hash-object --stdin
and check if we've previously seen it by querying for the hash in git with git cat-file -e
. If we haven't seen it, the remote picture must have changed, and we record it first in git with git hash-object -w
, to accommodate races between pulling and committing on the remote, and then proceed to fetch the remote.
One can integrate this with a git pre-fetch functionality: pre-fetch hook functionality in git, but that's out of the scope of this answer.
Addendum
Note that the above will generate loose objects in git that occasionally will need to be garbage collected with git gc
, and possibly --prune
explicitly.
Further, the above should work as long as commits are not rearranged on purpose in such a way that branch tips remain the same. This would be /quite uncommon/ and goes against git guidelines of changing pushed state, but hey, the worst thing that can happen is that you skip a fetch.
Also note that ls-remote
works on a single remote. To work with multiple remotes, you'll have to extend the script by generating a list of remotes with git remote show
and work with each one in turn.
这篇关于如何知道本地回购与远程回购不同,无需获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!