如何检查远程存储库是否已更改,是否需要进行拉取?
现在我使用这个简单的脚本:
git pull --dry-run | grep -q -v 'Already up-to-date.' && changed=1
但它相当重。
有没有更好的办法?理想的解决方案是检查所有远程分支,并返回已更改分支的名称以及每个分支中新提交的数量。
如何检查远程存储库是否已更改,是否需要进行拉取?
现在我使用这个简单的脚本:
git pull --dry-run | grep -q -v 'Already up-to-date.' && changed=1
但它相当重。
有没有更好的办法?理想的解决方案是检查所有远程分支,并返回已更改分支的名称以及每个分支中新提交的数量。
当前回答
如果你运行这个脚本,它将测试当前分支是否需要git拉:
#!/bin/bash
git fetch -v --dry-run 2>&1 |
grep -qE "\[up\s+to\s+date\]\s+$(
git branch 2>/dev/null |
sed -n '/^\*/s/^\* //p' |
sed -r 's:(\+|\*|\$):\\\1:g'
)\s+" || {
echo >&2 "Current branch need a 'git pull' before commit"
exit 1
}
将它作为Git钩子预提交来避免非常方便
Merge branch 'foobar' of url:/path/to/git/foobar into foobar
当你承诺之前拉。
要将此代码用作钩子,只需复制/粘贴脚本即可
.git/hooks/pre-commit
and
chmod +x .git/hooks/pre-commit
其他回答
首先使用git远程更新,使您的远程参考更新到最新。然后你可以做以下几件事中的一件,比如:
Git status -uno将告诉你你正在跟踪的分支是在前面、后面还是已经偏离。如果没有显示,则本地和远程是相同的。 Git show-branch *master会显示所有以master结尾的分支中的提交(例如master和origin/master)。
如果你在git远程更新(git remote -v update)中使用-v,你可以看到哪些分支被更新了,所以你真的不需要任何进一步的命令。
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're 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 ("local is ahead" and "same") as "don't need to pull".
你可以使用git rev-parse <ref>获得任何ref的提交id,所以你可以对master和origin/master这样做并比较它们。如果它们相等,那么分支是相同的。如果它们不相等,你想知道哪个在前面。使用git merge-base master origin/master会告诉你两个分支的共同祖先,如果它们没有分叉,这将是一个或另一个相同的。如果您得到三个不同的id,则分支已经分离。
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
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
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}代替。
UPSTREAM=${1:-'@{u}'}允许你显式地传递一个上游分支,以防你想检查一个不同于当前分支配置的远程分支。这通常是remotename/branchname的形式。如果不指定参数,默认值为@{u}。
该脚本假设您已经首先进行了git获取或git远程更新,以使跟踪分支更新到最新。我没有把这个构建到脚本中,因为它更灵活,能够将抓取和比较作为单独的操作,例如,如果你想比较而不抓取,因为你最近已经获取了。
我建议你去看看脚本https://github.com/badele/gitcheck。我编写了这个脚本,用于一次性检查所有Git存储库,它显示了谁没有提交,谁没有推/拉。
下面是一个示例结果:
我认为最好的方法是:
git diff remotes/origin/HEAD
假设您已经注册了this refspec。如果您已经克隆了存储库,则应该这样做,否则(例如,如果回购是在本地从头创建的,并推到远程),则需要显式地添加refspec。
所有这些复杂的建议,而解决方案却如此简单:
#!/bin/bash
BRANCH="<your branch name>"
LAST_UPDATE=`git show --no-notes --format=format:"%H" $BRANCH | head -n 1`
LAST_COMMIT=`git show --no-notes --format=format:"%H" origin/$BRANCH | head -n 1`
git remote update
if [ $LAST_COMMIT != $LAST_UPDATE ]; then
echo "Updating your branch $BRANCH"
git pull --no-edit
else
echo "No updates available"
fi
在阅读了许多答案和多个帖子,并花了半天时间尝试各种排列之后,这就是我想出的。
如果你在Windows上,你可以使用Git for Windows提供的Git Bash在Windows中运行这个脚本(安装或移植)。
这个脚本需要参数
- local path e.g. /d/source/project1 - Git URL e.g. https://username@bitbucket.org/username/project1.git - password if a password should not be entered on the command line in plain text, then modify the script to check if GITPASS is empty; do not replace and let Git prompt for a password
脚本将会
- Find the current branch
- Get the SHA1 of the remote on that branch
- Get the SHA1 of the local on that branch
- Compare them.
如果脚本打印了更改,那么您可以继续获取或拉取。脚本可能效率不高,但它为我完成了工作。
更新- 2015-10-30:stderr to dev null,防止将URL和密码打印到控制台。
#!/bin/bash
# Shell script to check if a Git pull is required.
LOCALPATH=$1
GITURL=$2
GITPASS=$3
cd $LOCALPATH
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
echo
echo git url = $GITURL
echo branch = $BRANCH
# Bash replace - replace @ with :password@ in the GIT URL
GITURL2="${GITURL/@/:$GITPASS@}"
FOO="$(git ls-remote $GITURL2 -h $BRANCH 2> /dev/null)"
if [ "$?" != "0" ]; then
echo cannot get remote status
exit 2
fi
FOO_ARRAY=($FOO)
BAR=${FOO_ARRAY[0]}
echo [$BAR]
LOCALBAR="$(git rev-parse HEAD)"
echo [$LOCALBAR]
echo
if [ "$BAR" == "$LOCALBAR" ]; then
#read -t10 -n1 -r -p 'Press any key in the next ten seconds...' key
echo No changes
exit 0
else
#read -t10 -n1 -r -p 'Press any key in the next ten seconds...' key
#echo pressed $key
echo There are changes between local and remote repositories.
exit 1
fi