我的想法是,为了只在上游推送了一些提交时才自动获取更新,我应该检查它的散列。
2种方法,我可以做的是,检查logs
或使用rev-list
的东西如下:
# this fetches top 30 commits from forked repo
git rev-list -n 30 main
# this also does the same but not limited to 30
git log --format='%H'
我发现很难比较哈希值,因为我无法从分支回购中获取超过1个提交。
下面解释了我的方法:
git remote add upstream https://github.com/upstream/upstream.git
git fetch upstream --tags
upstream_commit="$(git rev-parse upstream/main)"
echo "Upstream commit: ${upstream_commit}"
# This should store 30 recent commit hashes as an array AFAIK
forked_commits=($(git rev-list -n 30 main))
# another I tried to put forloop is this:
# for forked_commit in "$forked_commits[@]"; do
for forked_commit in $(git log --format='%H'); do
# but prints only the top most element and exits the loop
echo "$forked_commit"
if [ "$upstream_commit" == "$forked_commit" ]; then
has_new_commits=false
else
# since I've added my commits on top, if condition fails and this always returns true
has_new_commits=true
fi
done
if [ "${has_new_commits}" ]; then
git checkout main
git rebase upstream/main
git push -f origin main
echo "Rebase successful!"
else
echo "No commits to be synced!"
fi
1条答案
按热度按时间rqenqsqc1#
好吧,我犯了很多小错。
[ $has_new_commits ]; then
是错误的。我应该使用[ $has_new_commits == "true"]; then
。更正代码: