检查是否需要拉Git中 [英] Check if pull needed in Git

查看:109
本文介绍了检查是否需要拉Git中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查看远程仓库是否有变化,我需要拉?

How do I check whether the remote repository has changed and I need to pull?

现在我用这个简单的脚本:

Now I use this simple script:

git pull --dry-run | grep -q -v 'Already up-to-date.' && changed=1

但它是相当沉重的。

But it is rather heavy.

有没有更好的办法?理想的解决方案会检查所有远程分支机构,并在每一个返回改变了分支机构名称和新提交的数量。

Is there a better way? The ideal solution would check all the remote branches, and return names of the changed branches and the number of new commits in each one.

推荐答案

第一次使用 git的远程更新 ,带给您的远程裁判是最新的。然后,你可以做的几件事情,例如一个:

First use git remote update, to bring your remote refs up to date. Then you can do one of several things, such as:


  1. -uno git的状态会告诉你,你所跟踪的分支是否领先,后面或已发散。如果什么都不说,本地和远程都一样。

  1. git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.

混帐展示分支*主将显示您提交的所有后缀名为主分支(如主产地/主)。

git show-branch *master will show you the commits in all of the branches whose names end in master (eg master and origin/master).

如果您使用 -v git的远程更新远程Git -v更新),您可以看到哪些分支得到了更新,所以你并不需要任何进一步的命令。

If you use -v with git remote update (git remote -v update) you can see which branches got updated, so you don't really need any further commands.

不过,它看起来像你想这样做的脚本或程序,并用真/假值结束。如果是这样,有很多方法可以检查您当前HEAD提交你正在跟踪分支的负责人之间的关系,虽然因为有四种可能的结果则无法将其降低到是/否的答案。但是,如果你ppared做拉--rebase 然后你可以把本地的背后是和$ P $当地已发散为需要拉,而另两个为不需要拉

However, it looks like you want to do this in a script or program and end up with a true/false value. If so, there are ways to check the relationship between your current HEAD commit and the head of the branch you are tracking, although since there are four possible outcomes you can't reduce it to a yes/no answer. However, if you're prepared to do a pull --rebase then you can treat "local is behind" and "local has diverged" as "need to pull", and the other two as "don't need to pull".

您可以使用任何参考的提交ID git的REV-解析<参考> ,这样你就可以对主产地/主做到这一点并加以比较。如果它们相等,分支是相同的。如果他们是不平等的,你要知道哪个是领先于其他的。使用混帐合并基础主产地/主会告诉你两个分支的共同祖先,如果他们没有分歧,这将是同一种或另一种。如果你得到三个不同的ID,树枝有分歧。

You can get the commit id of any ref using git rev-parse <ref>, so you can do this for master and origin/master and compare them. If they are equal, the branches are the same. If they're unequal, you want to know which is ahead of the other. Using git merge-base master origin/master will tell you the common ancestor of both branches, and if they haven't diverged this will be the same as one or the other. If you get three different ids, the branches have diverged.

要做到这一点正确的,例如,在一个脚本,你必须能够引用当前分支和远程分支它的跟踪。在 /etc/bash_completion.d Bash提示符设置功能有一些有用的code获取分支名称。不过,你可能实际上并不需要获得的名称。 Git有一些巧妙的速记为参照分支机构和承诺(如记录在 git的REV-解析--help )。特别是,您可以使用 @ 当前分支(假设你在分离头的状态不是)和 @ {蓝} 其上游分支(如产地/主)。因此,混帐合并基础@ {蓝} 将返回(哈希)提交在其当前分支及其上游分支, git的转-parse @ git的REV-解析@ {蓝} 会给你两个提示的哈希值。这可以归纳为以下脚本:

To do this properly, eg in a script, you need to be able to refer to the current branch, and the remote branch it's tracking. The bash prompt-setting function in /etc/bash_completion.d has some useful code for getting branch names. However, you probably don't actually need to get the names. Git has some neat shorthands for referring to branches and commits (as documented in git rev-parse --help). In particular, you can use @ for the current branch (assuming you're not in a detached-head state) and @{u} for its upstream branch (eg origin/master). So git merge-base @ @{u} will return the (hash of) the commit at which the current branch and its upstream diverge and git rev-parse @ and git rev-parse @{u} will give you the hashes of the two tips. This can be summarized in the following script:

#!/bin/sh

LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{u})
BASE=$(git merge-base @ @{u})

if [ $LOCAL = $REMOTE ]; then
    echo "Up-to-date"
elif [ $LOCAL = $BASE ]; then
    echo "Need to pull"
elif [ $REMOTE = $BASE ]; then
    echo "Need to push"
else
    echo "Diverged"
fi

注:的老版本的Git没有让 @ 对自己,所以你可能必须使用 @ {0} 代替。

Note: older versions of git didn't allow @ on its own, so you may have to use @{0} instead.

该脚本还假定您已经做了混帐取 git的远程更新第一,把跟踪分支是最新的。我没有建立这个到脚本,因为它更灵活,能够做到的取得和作为单独的操作比较,例如,如果你想运行,免去因为你已经获取最近比较。

The script also assumes that you've done a git fetch or git remote update first, to bring the tracking branches up to date. I didn't build this into the script because it's more flexible to be able to do the fetching and the comparing as separate operations, for example if you want to compare without fetching because you already fetched recently.

这篇关于检查是否需要拉Git中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆